ballocbasicEMF

Synopsis

Allocate a dynamic block of memory.

Prototypes

 #include "webs.h" 

 void *balloc(B_ARGS_DEC, int size);

Parameters

B_ARGS_DECCaller's source file name and line number. Really two arguments.
sizeSize of the block to allocate.

Description

The balloc procedure creates and allocates a block of dynamic memory for use by the caller. When finished, you should use bfree to release the block. Balloc is a very efficient memory allocation that uses binary queues to store blocks of memory. It does not do block coalescing, nor does it attempt to eliminate fragmentation. Once released by bfree, memory is not actually freed to the operating system, but is kept on the relevant binary queue for future use by the application. Use B_L as the first argument to bfree to provide the source file name and line number arguments.

The goal of the balloc memory allocator is to provide guaranteed, rapid allocation. By calling bopen with the application memory requirements, memory can be defined or allocated upfront and subsequent calls to balloc are more likely to succeed. Balloc is not perfect. You can still get free memory on one class queue while memory requests for a large size cannot be satisfied. However, in practice, many embedded applications tend to allocate memory in a pattern and the balloc memory allocator works well in such environments. The memory usage of the GoAhead WebServer has been optimized to allocate from the fixed pool defined by bopen, and it should be very predictable in its requirements. The bstats procedure may be used to print current memory statistics include per file usage information.

Return Value

Pointer to the allocated buffer. If memory cannot be provided, NULL is returned.

Example

buf = balloc(B_L, 1024);
gstrncpy(buf, "Hello World", sizeof(buf));
bfree(B_L, buf);

Stability Classification

Stable.

See Also

bopen, bfree