The memory allocator does not track how many bytes you requested. Clib_mem_alloc(...) returns no less than the requested number of bytes. That’s the API, contract, or promise depending on how you want to put it.
Clib_mem_realloc(...) always allocates a fresh chunk of memory and copies the original data into the fresh chunk. It could use a clib_mem_size() check to attempt to skip the allocate-and-copy operation. If the original object is large enough to accommodate the realloc request, there’s no reason to do anything. However: clib_mem_realloc(...) is called in exactly one place in the code base, and it’s known by construction that the case in question requires a new object. Idiomatic vpp coding uses vectors instead of direct calls to the memory allocator. Vectors know how many elements they contain. A u8 * vector would likely amount to a drop-in replacement for whatever you’re doing. We won’t be changing the memory allocator API / contract / promise to track the original request size. Dave From: [email protected] <[email protected]> On Behalf Of [email protected] Sent: Friday, August 28, 2020 11:11 PM To: [email protected] Subject: Re: [vpp-dev] clib_mem_size return wrong mem size Hi, Dave Barach: I want to a realloc funtion like as bollow, the function args doesn't have the original mem size, and clib_mem_size seems can't return the right memory size. Is there any way to get the memory size ? void *my_realloc(void *p, size_t s) { return p ? clib_mem_realloc(p, s, clib_mem_size(p)) : clib_mem_alloc(s); }
-=-=-=-=-=-=-=-=-=-=-=- Links: You receive all messages sent to this group. View/Reply Online (#17301): https://lists.fd.io/g/vpp-dev/message/17301 Mute This Topic: https://lists.fd.io/mt/76466873/21656 Group Owner: [email protected] Unsubscribe: https://lists.fd.io/g/vpp-dev/unsub [[email protected]] -=-=-=-=-=-=-=-=-=-=-=-
