static WasmStackAllocatorChunk* allocate_chunk( WasmStackAllocator* stack_allocator, size_t max_avail, const char* file, int line) { assert(max_avail < SIZE_MAX - sizeof(WasmStackAllocatorChunk)); size_t real_size = max_avail + sizeof(WasmStackAllocatorChunk); /* common case of allocating a chunk of exactly CHUNK_SIZE */ if (real_size == CHUNK_SIZE) { if (stack_allocator->next_free) { WasmStackAllocatorChunk* chunk = stack_allocator->next_free; stack_allocator->next_free = stack_allocator->next_free->next_free; return chunk; } } WasmStackAllocatorChunk* chunk = wasm_alloc(stack_allocator->fallback, real_size, CHUNK_ALIGN); if (!chunk) { if (stack_allocator->has_jmpbuf) longjmp(stack_allocator->jmpbuf, 1); WASM_FATAL("%s:%d: memory allocation failed\n", file, line); } /* use the same allocation for the WasmStackAllocatorChunk and its data. + 1 * skips over the WasmStackAllocatorChunk */ chunk->start = chunk + 1; chunk->current = chunk->start; chunk->end = (void*)((intptr_t)chunk->start + max_avail); chunk->prev = NULL; #if WASM_STACK_ALLOCATOR_STATS stack_allocator->chunk_alloc_count++; stack_allocator->total_chunk_bytes += CHUNK_SIZE; #endif /* WASM_STACK_ALLOCATOR_STATS */ return chunk; }
void wasm_init_output_buffer(WasmAllocator* allocator, WasmOutputBuffer* buf, size_t initial_capacity) { assert(initial_capacity != 0); buf->allocator = allocator; buf->start = wasm_alloc(allocator, initial_capacity, WASM_DEFAULT_ALIGN); buf->size = 0; buf->capacity = initial_capacity; }
static char* get_module_filename(Context* ctx) { size_t buflen = ctx->json_filename_noext.length + 20; char* str = wasm_alloc(ctx->allocator, buflen, WASM_DEFAULT_ALIGN); size_t length = wasm_snprintf( str, buflen, PRIstringslice ".%" PRIzd ".wasm", WASM_PRINTF_STRING_SLICE_ARG(ctx->json_filename_noext), ctx->num_modules); convert_backslash_to_slash(str, length); return str; }