cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
alloc.h File Reference

Memory allocation wrappers backed by libttak. More...

#include <stddef.h>
#include <ttak/mem/owner.h>
#include <cwist/core/mem/arena.h>
Include dependency graph for alloc.h:

Go to the source code of this file.

Classes

struct  cwist_scratch_t
 Backing state for cwist_scratch_alloc()/CWIST_SCRATCH_DEFER: a single block-scoped allocation that prefers a recycled arena generation and falls back to cwist_alloc() when the request doesn't fit one (see cwist/core/mem/arena.h – only CWIST_ARENA_DEFAULT_GENERATION_BYTES-sized generations hit the zero-libttak-GC recycle path, so cwist_scratch_alloc() always requests the default size and never a custom one). More...
 

Macros

#define CWIST_ATTRIBUTE_CLEANUP(func)
 Variable cleanup attribute for GCC/Clang; no-op on other compilers (e.g. TinyCC), where the variable then needs an explicit cwist_free() like today.
 
#define CWIST_DEFER_FREE   CWIST_ATTRIBUTE_CLEANUP(cwist_defer_free_cb)
 Attach to a pointer variable's declaration to cwist_free() it automatically when that variable's enclosing block scope exits, on every return path (GCC/Clang; no-op on TinyCC).
 
#define cwist_alloc_scoped(var, cast, size)
 Declare var as a cast -typed pointer to a fresh cwist_alloc() block that is freed automatically when the enclosing block exits. Equivalent to combining cwist_alloc() with CWIST_DEFER_FREE, spelled as one declaration.
 
#define CWIST_SCRATCH_DEFER   CWIST_ATTRIBUTE_CLEANUP(cwist_scratch_cleanup)
 Attach to a cwist_scratch_t variable's declaration so its allocation (arena- or heap-backed, whichever cwist_scratch_alloc() picked) is released automatically when the enclosing block exits.
 

Functions

ttak_owner_t * cwist_create_owner (void)
 Lazily create (or return) the shared CWIST owner context.
 
void * cwist_malloc (size_t size)
 Allocate zeroed memory via the CWIST owner context.
 
void * cwist_alloc (size_t size)
 Allocate zeroed memory tracked by libttak.
 
void * cwist_alloc_array (size_t count, size_t elem_size)
 Allocate zeroed memory for count elements.
 
void * cwist_realloc (void *ptr, size_t new_size)
 Resize an existing allocation (libttak tracked).
 
char * cwist_strdup (const char *src)
 Duplicate a string using libttak-backed storage.
 
char * cwist_strndup (const char *src, size_t n)
 Duplicate up to n bytes of a string (libttak-backed).
 
void cwist_free (void *ptr)
 Free memory obtained via cwist_alloc/cwist_strdup/etc.
 

Detailed Description

Memory allocation wrappers backed by libttak.

Definition in file alloc.h.

Macro Definition Documentation

◆ cwist_alloc_scoped

#define cwist_alloc_scoped (   var,
  cast,
  size 
)
Value:
#define CWIST_DEFER_FREE
Attach to a pointer variable's declaration to cwist_free() it automatically when that variable's encl...
Definition alloc.h:101
void * cwist_alloc(size_t size)
Allocate zeroed memory tracked by libttak.
Definition alloc.c:17
size_t cap
Definition otel.c:56

Declare var as a cast -typed pointer to a fresh cwist_alloc() block that is freed automatically when the enclosing block exits. Equivalent to combining cwist_alloc() with CWIST_DEFER_FREE, spelled as one declaration.

cwist_alloc_scoped(name, char *, 64);
snprintf(name, 64, "...");
// name is cwist_free()'d automatically here
#define cwist_alloc_scoped(var, cast, size)
Declare var as a cast -typed pointer to a fresh cwist_alloc() block that is freed automatically when ...
Definition alloc.h:116

Definition at line 116 of file alloc.h.

◆ CWIST_ATTRIBUTE_CLEANUP

#define CWIST_ATTRIBUTE_CLEANUP (   func)

Variable cleanup attribute for GCC/Clang; no-op on other compilers (e.g. TinyCC), where the variable then needs an explicit cwist_free() like today.

Mirrors TTAK_ATTRIBUTE_CLEANUP in lib/libttak/include/ttak/types/ttak_compiler.h.

Definition at line 64 of file alloc.h.

◆ CWIST_DEFER_FREE

#define CWIST_DEFER_FREE   CWIST_ATTRIBUTE_CLEANUP(cwist_defer_free_cb)

Attach to a pointer variable's declaration to cwist_free() it automatically when that variable's enclosing block scope exits, on every return path (GCC/Clang; no-op on TinyCC).

Usage:

void *buf CWIST_DEFER_FREE = cwist_alloc(256);
if (something) return; // buf is freed here too
...
// and freed here, at the end of the block it was declared in

Only safe for a pointer that does not outlive the block it is declared in (i.e. never stored into a struct field, global, or queued job argument that another function/thread/job will read later). A pointer that does escape must not use this – see cwist_gc_scope_track() / cwist_gc_scope_disown() in cwist/core/mem/gc.h instead, which handle lifetimes that cross those boundaries.

Definition at line 101 of file alloc.h.

◆ CWIST_SCRATCH_DEFER

#define CWIST_SCRATCH_DEFER   CWIST_ATTRIBUTE_CLEANUP(cwist_scratch_cleanup)

Attach to a cwist_scratch_t variable's declaration so its allocation (arena- or heap-backed, whichever cwist_scratch_alloc() picked) is released automatically when the enclosing block exits.

unsigned char *buf = cwist_scratch_alloc(&buf_s, needed);
if (!buf) return -1;
...
// buf_s's backing storage is released here, on every return path
#define CWIST_SCRATCH_DEFER
Attach to a cwist_scratch_t variable's declaration so its allocation (arena- or heap-backed,...
Definition alloc.h:190
Backing state for cwist_scratch_alloc()/CWIST_SCRATCH_DEFER: a single block-scoped allocation that pr...
Definition alloc.h:131

Same escape rule as CWIST_DEFER_FREE: only for a pointer that does not outlive the block cwist_scratch_t was declared in.

Definition at line 190 of file alloc.h.

Function Documentation

◆ cwist_alloc()

void * cwist_alloc ( size_t  size)

Allocate zeroed memory tracked by libttak.

Definition at line 17 of file alloc.c.

References cwist_malloc().

Referenced by base64_encode(), cwist_app_asset_add(), cwist_app_asset_add_file(), cwist_app_create(), cwist_app_enable_openapi(), cwist_app_grpc_health(), cwist_app_grpc_health_set_status(), cwist_app_http_handler_async(), cwist_app_register_error_handler(), cwist_app_static(), cwist_app_static_with_cache(), cwist_assets_clone(), cwist_async_defer(), cwist_bdr_copy_get(), cwist_bdr_create(), cwist_config_create(), cwist_config_load_env(), cwist_config_set(), cwist_cookie_encode(), cwist_css_minify(), cwist_db_open(), cwist_db_open_memory(), cwist_db_pool_create(), cwist_db_serialize(), cwist_ebr_alloc(), cwist_flash_pop_all_json(), cwist_graphql_event_source_create(), cwist_graphql_schema_create(), cwist_graphql_ws_add_subscription(), cwist_graphql_ws_create(), cwist_grpc_call_start_ex(), cwist_grpc_channel_apply_service_config_json(), cwist_grpc_channel_connect(), cwist_grpc_channel_unary(), cwist_grpc_client_connect(), cwist_grpc_decoder_feed(), cwist_grpc_encode_message(), cwist_grpc_routes_clone(), cwist_h2_async_queue_enqueue(), cwist_html_component_create(), cwist_html_element_create(), cwist_http2_serve_connection_ex(), cwist_http3_init_context(), cwist_http3_init_context_ephemeral(), cwist_http_async_rearm(), cwist_http_async_send_response(), cwist_http_client_create(), cwist_http_coalesce_append(), cwist_http_coalesce_flush(), cwist_http_pool_init_mode(), cwist_http_pool_submit(), cwist_http_pool_submit_async(), cwist_http_receive_request(), cwist_http_receive_request_nb(), cwist_http_response_send_file(), cwist_http_response_serialize(), cwist_http_send_response_async(), cwist_http_send_response_coalesced(), cwist_http_sendmsg_speculative(), cwist_https_init_context_with_options(), cwist_https_receive_request(), cwist_https_send_response(), cwist_io_queue_create(), cwist_io_queue_submit(), cwist_job_queue_create_redis(), cwist_json_builder_create(), cwist_jwt_join_chunks(), cwist_log_async_start(), cwist_logger_create(), cwist_lwip_listen(), cwist_lwip_tls_create(), cwist_metrics_registry(), cwist_multipart_extract_boundary(), cwist_multipart_parse(), cwist_mux_group_create(), cwist_mux_group_handle(), cwist_mux_handle(), cwist_mux_route_use(), cwist_mux_router_create(), cwist_nats_connect(), cwist_openapi_annotate(), cwist_reactor_create(), cwist_reactor_timer_arm(), cwist_redis_connect(), cwist_redis_pool_create(), cwist_sanitize_html(), cwist_scheduler_create(), cwist_seq_assembler_create_limited(), cwist_seq_assembler_feed(), cwist_seq_auth_context_create(), cwist_seq_auth_wrap(), cwist_seq_split(), cwist_session_start(), cwist_sse_stream_open(), cwist_sstring_create(), cwist_sstring_substr(), cwist_stream_req_begin(), cwist_stream_req_feed(), cwist_template_render_file(), cwist_test_client_create(), cwist_test_client_post_multipart(), cwist_test_client_set_cookie(), cwist_webrtc_conn_sctp_out(), cwist_webrtc_conn_send(), cwist_websocket_async_attach_ex(), cwist_websocket_receive(), cwist_websocket_receive_sequenced(), cwist_websocket_upgrade(), h2_decode_string(), h2_huffman_decode(), and session_shared_alloc().

◆ cwist_alloc_array()

◆ cwist_create_owner()

ttak_owner_t * cwist_create_owner ( void  )

Lazily create (or return) the shared CWIST owner context.

◆ cwist_free()

void cwist_free ( void *  ptr)

Free memory obtained via cwist_alloc/cwist_strdup/etc.

Definition at line 52 of file alloc.c.

Referenced by cwist_app_asset_add_file(), cwist_app_asset_prefix(), cwist_app_create(), cwist_app_destroy(), cwist_app_enable_openapi(), cwist_app_grpc_health(), cwist_app_grpc_health_set_status(), cwist_app_http_handler_async(), cwist_app_reload_tls(), cwist_app_set_session_name(), cwist_app_set_tls_groups(), cwist_app_static(), cwist_app_static_with_cache(), cwist_app_use_db(), cwist_app_use_https(), cwist_app_use_nuke_db(), cwist_app_use_session(), cwist_assets_clone(), cwist_assets_destroy(), cwist_async_defer(), cwist_async_release(), cwist_bdr_create(), cwist_bdr_destroy(), cwist_config_create(), cwist_config_destroy(), cwist_config_set(), cwist_cookie_parse(), cwist_cookie_set(), cwist_css_minify(), cwist_css_scope_add_rule(), cwist_css_scope_destroy(), cwist_db_close(), cwist_db_open(), cwist_db_open_memory(), cwist_db_pool_create(), cwist_db_pool_destroy_timeout(), cwist_flash_get(), cwist_flash_pop_all_json(), cwist_graphql_event_source_add_topic(), cwist_graphql_event_source_destroy(), cwist_graphql_publish(), cwist_graphql_schema_destroy(), cwist_graphql_ws_add_subscription(), cwist_graphql_ws_destroy(), cwist_grpc_call_destroy(), cwist_grpc_call_recv(), cwist_grpc_call_start_ex(), cwist_grpc_channel_apply_service_config_json(), cwist_grpc_channel_call_destroy(), cwist_grpc_channel_close(), cwist_grpc_channel_connect(), cwist_grpc_channel_unary(), cwist_grpc_client_close(), cwist_grpc_client_connect(), cwist_grpc_decoder_destroy(), cwist_grpc_decoder_feed(), cwist_grpc_routes_destroy(), cwist_grpc_set_response(), cwist_grpc_stream_recv(), cwist_grpc_stream_send(), cwist_h2_async_queue_release(), cwist_html_component_create(), cwist_html_component_destroy(), cwist_html_element_destroy(), cwist_http2_serve_connection_ex(), cwist_http2_stream_send_headers(), cwist_http2_stream_send_trailers(), cwist_http3_destroy_context(), cwist_http_async_rearm(), cwist_http_async_send_response(), cwist_http_client_create(), cwist_http_client_destroy(), cwist_http_client_set_altsvc_db(), cwist_http_client_set_ca_bundle(), cwist_http_coalesce_append(), cwist_http_coalesce_flush(), cwist_http_header_free_all(), cwist_http_header_remove(), cwist_http_pool_destroy(), cwist_http_pool_init_mode(), cwist_http_receive_request(), cwist_http_request_destroy(), cwist_http_response_destroy(), cwist_http_response_send_file(), cwist_http_response_set_alt_svc(), cwist_http_send_response_async(), cwist_http_send_response_coalesced(), cwist_http_sendmsg_speculative(), cwist_https_destroy_context(), cwist_https_receive_request(), cwist_https_send_response(), cwist_io_queue_create(), cwist_io_queue_destroy(), cwist_io_queue_run(), cwist_io_queue_submit(), cwist_job_queue_create_redis(), cwist_job_queue_destroy(), cwist_json_builder_create(), cwist_json_builder_destroy(), cwist_json_heal(), cwist_jwt_chunks_free(), cwist_jwt_claims_destroy(), cwist_jwt_sign_chunks(), cwist_jwt_verify_chunks(), cwist_log_async_start(), cwist_log_async_stop(), cwist_logger_create(), cwist_logger_destroy(), cwist_lwip_close(), cwist_lwip_listen(), cwist_lwip_tls_destroy(), cwist_make_socket_ipv4(), cwist_mtls_EVP_CIPHER_CTX_free(), cwist_mtls_HMAC_CTX_free(), cwist_mtls_SSL_CTX_new(), cwist_mtls_SSL_CTX_set_tlsext_ticket_key_cb(), cwist_mtls_SSL_free(), cwist_mtls_SSL_new(), cwist_multipart_parse(), cwist_multipart_result_destroy(), cwist_mux_group_create(), cwist_mux_group_destroy(), cwist_mux_router_create(), cwist_mux_router_destroy(), cwist_mw_otel_trace(), cwist_nats_connect(), cwist_nats_destroy(), cwist_openapi_annotate(), cwist_pb_writer_free(), cwist_reactor_create(), cwist_reactor_destroy(), cwist_reactor_run(), cwist_reactor_timer_arm(), cwist_redis_auth(), cwist_redis_close(), cwist_redis_command(), cwist_redis_command_argv(), cwist_redis_del(), cwist_redis_expire(), cwist_redis_pool_command_argv_reply_timeout(), cwist_redis_pool_create(), cwist_redis_pool_destroy(), cwist_redis_publish(), cwist_redis_reply_free(), cwist_redis_select(), cwist_redis_set(), cwist_redis_setex(), cwist_redis_subscribe(), cwist_scheduler_cancel_pending(), cwist_scheduler_create(), cwist_scheduler_destroy(), cwist_sctp_conn_close(), cwist_seq_assembler_destroy(), cwist_seq_assembler_feed(), cwist_seq_assembler_reset(), cwist_seq_auth_context_create(), cwist_seq_auth_context_destroy(), cwist_seq_auth_wrap(), cwist_seq_message_free(), cwist_seq_split(), cwist_session_commit(), cwist_session_destroy(), cwist_session_start(), cwist_sse_stream_close(), cwist_sstring_adopt_len(), cwist_sstring_adopt_region(), cwist_sstring_borrow(), cwist_sstring_destroy(), cwist_stream_req_begin(), cwist_stream_req_dispatch(), cwist_stream_req_feed(), cwist_test_client_destroy(), cwist_test_client_post_multipart(), cwist_test_client_set_cookie(), cwist_webrtc_conn_release(), cwist_websocket_async_attach_ex(), cwist_websocket_destroy(), cwist_websocket_frame_destroy(), cwist_websocket_receive(), cwist_websocket_upgrade_response(), h2_huffman_decode(), https_pool_destroy(), and session_shared_dec().

◆ cwist_malloc()

◆ cwist_realloc()

void * cwist_realloc ( void *  ptr,
size_t  new_size 
)

Resize an existing allocation (libttak tracked).

Definition at line 27 of file alloc.c.

References cwist_malloc().

Referenced by cwist_app_asset_add_file(), cwist_flash_pop_all_json(), cwist_html_element_add_child(), cwist_http2_stream_send_trailers(), cwist_pb_writer_reserve(), and h2_huffman_decode().

◆ cwist_strdup()

◆ cwist_strndup()

char * cwist_strndup ( const char *  src,
size_t  n 
)

Duplicate up to n bytes of a string (libttak-backed).

Definition at line 41 of file alloc.c.

References cwist_malloc().