#1 Better uthash_free() support for embedded systems

closed
nobody
None
5
2010-06-08
2010-05-20
No

Hi,

It would be really nice if uthash passed the size of the allocation being freed to the uthash_free() macro as in bootstrap memory allocator situations (e.g. on simple embedded systems) the bootstrap memory allocator doesn't store the allocation's size with the block, so you must tell it the size to free as well.

Luckily this is very easily accomplished as the patch below implements:

--- D:\Users\ned\AppData\Local\Temp\uthash.-revBASE.svn001.tmp.h 2010-05-20 19:49:11.000000000 +-0100
+++ D:\Users\ned\Documents\Left Leaning Red Black Trees\Left Leaning Red Black Trees\uthash\src\uthash.h 2010-05-20 19:49:41.000000000 +-0100
@@ -37,13 +38,13 @@
#define TYPEOF(x)
#endif

#define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */
#define uthash_malloc(sz) malloc(sz) /* malloc fcn */
-#define uthash_free(ptr) free(ptr) /* free fcn */
+#define uthash_free(ptr, sz) free((ptr)) /* free fcn */

#define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */
#define uthash_expand_fyi(tbl) /* can be defined to log expands */

/* initial number of buckets */
#define HASH_INITIAL_NUM_BUCKETS 32 /* initial number of buckets */
@@ -77,13 +78,13 @@
memset((tbl)->bloom_bv, 0, HASH_BLOOM_BYTELEN); \ (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \ } while (0);

#define HASH_BLOOM_FREE(tbl) \ do { \ - uthash_free((tbl)->bloom_bv); \ + uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \ } while (0);

#define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8] |= (1U << ((idx)%8)))
#define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8] & (1U << ((idx)%8)))

#define HASH_BLOOM_ADD(tbl,hashv) \ @@ -165,15 +166,16 @@
*/
#define HASH_DELETE(hh,head,delptr) \ do { \ unsigned _hd_bkt; \ struct UT_hash_handle *_hd_hh_del; \ if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \ - uthash_free((head)->hh.tbl->buckets ); \ + uthash_free((head)->hh.tbl->buckets, \ + (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket) );\ HASH_BLOOM_FREE((head)->hh.tbl); \ - uthash_free((head)->hh.tbl); \ + uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ head = NULL; \ } else { \ _hd_hh_del = &((delptr)->hh); \ if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \ (head)->hh.tbl->tail = \ (UT_hash_handle*)((char*)((delptr)->hh.prev) + \ @@ -700,15 +702,15 @@
if (_he_newbkt->hh_head) _he_newbkt->hh_head->hh_prev = \ _he_thh; \ _he_newbkt->hh_head = _he_thh; \ _he_thh = _he_hh_nxt; \ } \ } \ + uthash_free( tbl->buckets, tbl->num_buckets*sizeof(struct UT_hash_bucket) );\ tbl->num_buckets *= 2; \ tbl->log2_num_buckets++; \ - uthash_free( tbl->buckets ); \ tbl->buckets = _he_new_buckets; \ tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \ (tbl->ineff_expands+1) : 0; \ if (tbl->ineff_expands > 1) { \ tbl->noexpand=1; \ uthash_noexpand_fyi(tbl); \ @@ -843,14 +845,15 @@
HASH_FSCK(hh_dst,dst); \ } while (0)

#define HASH_CLEAR(hh,head) \ do { \ if (head) { \ - uthash_free((head)->hh.tbl->buckets ); \ - uthash_free((head)->hh.tbl); \ + uthash_free((head)->hh.tbl->buckets, \ + (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \ + uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ (head)=NULL; \ } \ } while(0)

/* obtain a count of items in the hash */
#define HASH_COUNT(head) HASH_CNT(hh,head)

Thanks,
Niall

Discussion

  • Niall Douglas

    Niall Douglas - 2010-05-20

    Patch implementing the enhancement

     
  • Niall Douglas

    Niall Douglas - 2010-05-20

    I realised just there that copy & paste didn't work so well, so I've attached the patch. It's against v1.8 rather than v1.9.1 as for some reason my subversion hasn't picked up the SVN repo changes yet :(

    Thanks,
    Niall

     
  • Niall Douglas

    Niall Douglas - 2010-05-20

    Ah, I've just realised that the sourceforge uthash SVN hasn't been updated with v1.9.1 and is still stuck on v1.8. Can you fix this please?

    BTW I made the move to GIT a month ago, and it makes keeping the SF repo up to date with my local main repo very considerably easier indeed.

     
  • Troy Hanson

    Troy Hanson - 2010-06-08

    For starters I've synced the svn repo on SF to match uthash 1.9.1. The master repo is on my own git server these days.

     
  • Troy Hanson

    Troy Hanson - 2010-06-08

    Ok I've incorporated your patch into uthash in the SVN repo and my git master. I have no plans to release uthash 1.9.x anytime soon but it's there for when that day comes. Thanks.

     
  • Troy Hanson

    Troy Hanson - 2010-06-08
    • status: open --> closed