In the following I only talk about my_compress but the same thing is true about my_uncompress: The current my_compress functions does a malloc/free for every compress and this is quite inefficent. What is worse is that my_compress copies the compressed result back to the sent buffer, which may not be needed at all! A better approach would be to send to my_compress an indirect pointer to a malloced area and a pointer to the length of this area. my_compress() would then use this area or reallocate it if needed and the caller would be responsible for allocating it. my_compress() should be able to handle the case where the used_space == 0, in which case my_compress() should allocate the buffer. Note that my_realloc() can handle this case with the MY_ALLOW_ZERO_PTR flag!