4 ms·
Sure. Imagine you have a process with 2^20 active allocations (e.g. 2^20 calls to malloc()), i.e. you have a tree with the metainformation, and every time you d
by faragon 10y ago
Sure. Imagine you have a process with 2^20 active allocations (e.g. 2^20 calls to malloc()), i.e. you have a tree with the metainformation, and every time you de-allocate and allocate you have to search trough one or more trees. So no matter how "smart" is your library for avoiding OS system calls, you already have a hell to maintain (search through a tree or a list, delete, split, etc.). Things get ugly when a process has lots of dynamic memory calls, on non-trivial cases.
- vertex-four 10y agoAn alternative, if you have a well-defined portion of code by the end of which you know you're not going to touch any data which was allocated in it, is to use arenas - allocation is then a pointer bump, and you can deallocate all of it at once.
- faragon 10y agoOr even better: for local small allocations just use the stack.
- vertex-four 10y agoAlloca is non-standard and apparently a C11 compliant compiler can choose not to implement variable-length arrays - there's no standard way of allocating on the stack. Also - are you utterly certain your allocations will always be small? Could someone craft some input to your program to have you allocate more, and possibly use up the stack in some environments? There's environments which have much smaller stacks than have RAM available.
- faragon 10y agoYou can do stack allocation in C without "alloca()", e.g. size_t example(const char in, const size_t in_size, char out, const size_t max_out_size) { size_t out_size = 0; char stack_buf[32768]; char *buf = stack_buf; char *heap_buf = NULL; size_t req_size = in_size * 2; if (req_size < sizeof(stack_buf)) { buf = heap_buf = (char *)malloc(req_size); } // Do stuff if (heap_buf) { free(heap_buf); } return out_size; }
- vertex-four 10y agoThat's precisely what's being done in curl. Generally when people say "stack allocation" as an alternative to "malloc" they mean alloca/VLA, so you'll forgive my confusion.
- faragon 10y agoERRATA: if (req_size < sizeof(stack_buf)) Should be: if (req_size > sizeof(stack_buf))
- maccard 10y agoYou don't always know up front how much space you need.
- faragon 10y agoYou can guess a maximum for covering 99.99% of the cases, and use dynamic memory just in the 0.01%. E.g. size_t example(const char in, const size_t in_size, char out, const size_t max_out_size) { size_t out_size = 0; char stack_buf[32768]; char *buf = stack_buf; char *heap_buf = NULL; size_t req_size = in_size * 2; if (req_size < sizeof(stack_buf)) { buf = heap_buf = (char *)malloc(req_size); } // Do stuff if (heap_buf) { free(heap_buf); } return out_size; }
- faragon 10y agoERRATA: if (req_size < sizeof(stack_buf)) Should be: if (req_size > sizeof(stack_buf))
- maccard 10y ago> you have a tree with the metainformation, and every time you de-allocate and allocate you have to search trough one or more trees Don't use a tree in that case? I don't see why you using a tree to store allocation info means that allocations are slow.
- faragon 10y agoWhatever other data structure you use is not going to be simple, and will have even worse drawbacks: e.g. using hash tables for handling allocation pools and free blocks could be even worse.