how to modify uthash for use with static memory allocation

256 views
Skip to first unread message

ron.e...@gmail.com

unread,
Jul 7, 2016, 8:25:34 AM7/7/16
to uthash
up vote
0
down vote
favorite
I would like to utilize uthash for some hash tables in my system but I need all memory to be pre-allocated statically. How can I modify uthash to work with pre-allocated memory? I now have the following (which uses dynamic allocation with malloc())
[code]
#include "uthash.h"
#include <stdio.h>

struct my_struct {
int label; /* we'll use this field as the key */
int bfd;
UT_hash_handle hh; /* makes this structure hashable */
};

struct my_struct *users = NULL;

void add_user(int label, int bfd) {
struct my_struct *s;

s = malloc(sizeof(struct my_struct));
s->label = label;
s->bfd = bfd;
HASH_ADD_INT( users, label, s ); /* id: name of key field */
}

struct my_struct *find_user(int label) {
static struct my_struct *s;

HASH_FIND_INT( users, &label, s ); /* s: output pointer */
return s;
}

int main (void)
{
static struct my_struct *res = NULL;
add_user(123456,1);
add_user(12345,2);
add_user(1234567,3);

res = find_user(12345);
printf("12345 %d\n",res->bfd);
res = find_user(1234567);
printf("1234567 %d\n",res->bfd);
res = find_user(123456);
printf("123456 %d\n",res->bfd);

return 0;

}
[/code]

Troy D. Hanson

unread,
Jul 7, 2016, 8:47:46 AM7/7/16
to uthash, ron.e...@gmail.com
The basic idea is twofold: you create a pool of your structure and use that instead of malloc, and second you redefine the uthash_malloc and uthash_free hooks. That takes care of uthash's internal memory allocation. 

will1...@gmail.com

unread,
Sep 18, 2019, 8:11:25 PM9/18/19
to uthash
Hi Troy, I'm also looking to do this. Could you elaborate on how to do this? What do you mean by a "pool" and how should the hooks be modified?

Thanks


On Thursday, July 7, 2016 at 5:47:46 AM UTC-7, Troy D. Hanson wrote:
The basic idea is twofold: you create a pool of your structure and use that instead of malloc, and second you redefine the uthash_malloc and uthash_free hooks. That takes care of uthash's internal memory allocation. 

will1...@gmail.com

unread,
Sep 19, 2019, 1:03:46 PM9/19/19
to uthash
After looking around some more it seems like I need to create a fixed size memory pool and provide malloc/free functions that use that memory pool. Does that sound right?

Thanks
Message has been deleted

will1...@gmail.com

unread,
Sep 19, 2019, 1:37:05 PM9/19/19
to uthash
How would you go about redefining uthash_malloc and uthash_free to use static allocation?


On Thursday, July 7, 2016 at 5:47:46 AM UTC-7, Troy D. Hanson wrote:
The basic idea is twofold: you create a pool of your structure and use that instead of malloc, and second you redefine the uthash_malloc and uthash_free hooks. That takes care of uthash's internal memory allocation. 

Troy D. Hanson

unread,
Sep 22, 2019, 7:53:47 PM9/22/19
to uthash


On Thursday, September 19, 2019 at 1:37:05 PM UTC-4, will1...@gmail.com wrote:
How would you go about redefining uthash_malloc and uthash_free to use static allocation?

The first step is to use the hooks described in the link below to set up your own malloc and free functions. See tests/test92.c for an example. To allocate statically you would need to keep some kind of table of available memory and then return buffers to/from it. There are many ways you could do it, so that's a very generic answer. It boils down to bookkeeping. You could start by using hooks that wrap the system malloc and free with some printf statements to see how what kind of allocations and frees are made.

HTH. 

will1...@gmail.com

unread,
Sep 26, 2019, 2:44:15 PM9/26/19
to uthash
Thanks Troy, got it working!
Reply all
Reply to author
Forward
0 new messages