fix shadow variable + update comments + free in realloc
#include <unistd.h>
#include "picoquic_internal.h"
+/* This implementation is mostly a translation from C++ to C of the
+ * implementation proposed by Ben Kenwright in "Fast Efficient
+ * Fixed-Size Memory Pool: No Loops and No Overhead", presented at
+ * COMPUTATION TOOLS 2012.
+ */
+
#define MAGIC_NUMBER 0xa110ca7ab1e
uint8_t *addr_from_index(memory_pool_t *mp, uint64_t i) {
@@ -16,9 +22,8 @@ uint64_t index_from_addr(memory_pool_t *mp, uint8_t *p) {
/**
* Search for big enough free space on heap.
-* Split the free space slot if it is too big, else space will be wasted.
* Return the pointer to this slot.
-* If no adequately large free slot is available, extend the heap and return the pointer.
+* If no adequately large free slot is available, return NULL.
*/
void *my_malloc(picoquic_cnx_t *cnx, unsigned int size) {
protoop_plugin_t *p = cnx->current_plugin;
@@ -32,9 +37,9 @@ void *my_malloc(picoquic_cnx_t *cnx, unsigned int size) {
return NULL;
}
if (mp->num_initialized < mp->num_of_blocks) {
- uint64_t *p = (uint64_t *) addr_from_index(mp, mp->num_initialized);
+ uint64_t *ptr = (uint64_t *) addr_from_index(mp, mp->num_initialized);
/* Very important for the mp->next computation */
- *p = mp->num_initialized + 1;
+ *ptr = mp->num_initialized + 1;
mp->num_initialized++;
}
@@ -87,7 +92,7 @@ void my_free_in_core(protoop_plugin_t *p, void *ptr) {
* Frees the allocated memory. If first checks if the pointer falls
* between the allocated heap range. It also checks if the pointer
* to be deleted is actually allocated. this is done by using the
- * magic number. Due to lack of time i haven't worked on fragmentation.
+ * magic number.
*/
void my_free(picoquic_cnx_t *cnx, void *ptr) {
protoop_plugin_t *p = cnx->current_plugin;
@@ -103,11 +108,9 @@ void my_free_dbg(picoquic_cnx_t *cnx, void *ptr, char *file, int line) {
}
/**
* Reallocate the allocated memory to change its size. Three cases are possible.
- * 1) Asking for lower or equal size, or larger size without any block after.
- * The block is left untouched, we simply increase its size.
- * 2) Asking for larger size, and another block is behind.
- * We need to request another larger block, then copy the data and finally free it.
- * 3) Asking for larger size, without being able to have free space.
+ * 1) Asking for size lower than the block_size.
+ * The block is left untouched, we simply return it.
+ * 2) Asking for larger size, without being able to have free space.
* Free the pointer and return NULL.
* If an invalid pointer is provided, it returns NULL without changing anything.
*/
@@ -121,6 +124,8 @@ void *my_realloc(picoquic_cnx_t *cnx, void *ptr, unsigned int size) {
// otherwise, return the same pointer
if (size > p->memory_pool.size_of_each_block - 8) {
printf("Asking for %u bytes by slots up to %lu!\n", size, p->memory_pool.size_of_each_block - 8);
+ /* Don't forget to free the pointer! */
+ my_free_in_core(p, ptr);
return NULL;
}
return ptr;
