5aad932002-08-15Marcus Comstedt /* $Id: block_alloc.h,v 1.41 2002/08/15 14:49:19 marcus Exp $ */
4218011999-01-31Fredrik Hübinette (Hubbe) #undef PRE_INIT_BLOCK #undef INIT_BLOCK #undef EXIT_BLOCK #undef BLOCK_ALLOC
afbc722001-09-06Fredrik Hübinette (Hubbe) #undef LOW_PTR_HASH_ALLOC #undef PTR_HASH_ALLOC_FIXED
fc2c831999-04-01Fredrik Hübinette (Hubbe) #undef PTR_HASH_ALLOC
cfa7d82000-01-27Fredrik Hübinette (Hubbe) #undef COUNT_BLOCK #undef COUNT_OTHER
7d63aa2001-11-08Fredrik Hübinette (Hubbe) #undef BLOCK_ALLOC_HSIZE_SHIFT
4218011999-01-31Fredrik Hübinette (Hubbe) 
2bfcfe1998-03-27Fredrik Hübinette (Hubbe) #define PRE_INIT_BLOCK(X)
21ed0e1998-02-19Fredrik Hübinette (Hubbe) #define INIT_BLOCK(X) #define EXIT_BLOCK(X)
cfa7d82000-01-27Fredrik Hübinette (Hubbe) #define COUNT_BLOCK(X) #define COUNT_OTHER()
7d63aa2001-11-08Fredrik Hübinette (Hubbe) #define BLOCK_ALLOC_HSIZE_SHIFT 2
4218011999-01-31Fredrik Hübinette (Hubbe) 
aad99b2001-03-28Fredrik Hübinette (Hubbe) #ifdef PIKE_RUN_UNLOCKED #include "threads.h"
b076ce2001-08-31Fredrik Hübinette (Hubbe)  /* Block Alloc UnLocked */ #define BA_UL(X) PIKE_CONCAT(X,_unlocked) #define BA_STATIC static
0f5ac12001-09-02Henrik Grubbström (Grubba) #define BA_INLINE inline
b076ce2001-08-31Fredrik Hübinette (Hubbe) #else #define BA_UL(X) X #define BA_STATIC
0f5ac12001-09-02Henrik Grubbström (Grubba) #define BA_INLINE
aad99b2001-03-28Fredrik Hübinette (Hubbe) #endif
fc2c831999-04-01Fredrik Hübinette (Hubbe) #define BLOCK_ALLOC(DATA,BSIZE) \ \ struct PIKE_CONCAT(DATA,_block) \ { \ struct PIKE_CONCAT(DATA,_block) *next; \ struct DATA x[BSIZE]; \ }; \ \ static struct PIKE_CONCAT(DATA,_block) *PIKE_CONCAT(DATA,_blocks)=0; \
aad99b2001-03-28Fredrik Hübinette (Hubbe) static struct DATA *PIKE_CONCAT3(free_,DATA,s)=(struct DATA *)-1; \ DO_IF_RUN_UNLOCKED(static PIKE_MUTEX_T PIKE_CONCAT(DATA,_mutex);) \
fc2c831999-04-01Fredrik Hübinette (Hubbe)  \
ed3a5b2001-09-01Fredrik Hübinette (Hubbe) static void PIKE_CONCAT(alloc_more_,DATA)(void) \ { \ struct PIKE_CONCAT(DATA,_block) *n; \ int e; \ n=(struct PIKE_CONCAT(DATA,_block) *) \ malloc(sizeof(struct PIKE_CONCAT(DATA,_block))); \ if(!n) \
fc2c831999-04-01Fredrik Hübinette (Hubbe)  { \
ed3a5b2001-09-01Fredrik Hübinette (Hubbe)  fprintf(stderr,"Fatal: out of memory.\n"); \ exit(17); \ } \ n->next=PIKE_CONCAT(DATA,_blocks); \ PIKE_CONCAT(DATA,_blocks)=n; \
fc2c831999-04-01Fredrik Hübinette (Hubbe)  \
ed3a5b2001-09-01Fredrik Hübinette (Hubbe)  for(e=0;e<BSIZE;e++) \ { \ n->x[e].BLOCK_ALLOC_NEXT=(void *)PIKE_CONCAT3(free_,DATA,s); \ PRE_INIT_BLOCK( (n->x+e) ); \ PIKE_CONCAT3(free_,DATA,s)=n->x+e; \
fc2c831999-04-01Fredrik Hübinette (Hubbe)  } \
ed3a5b2001-09-01Fredrik Hübinette (Hubbe) } \ \
0f5ac12001-09-02Henrik Grubbström (Grubba) BA_STATIC BA_INLINE struct DATA *BA_UL(PIKE_CONCAT(alloc_,DATA))(void) \
ed3a5b2001-09-01Fredrik Hübinette (Hubbe) { \ struct DATA *tmp; \ if(!PIKE_CONCAT3(free_,DATA,s)) \ PIKE_CONCAT(alloc_more_,DATA)(); \
9820432001-07-01Martin Stjernholm  DO_IF_DEBUG( \ else if (PIKE_CONCAT3(free_,DATA,s) == (struct DATA *)-1) \
5aad932002-08-15Marcus Comstedt  Pike_fatal("Block alloc not initialized.\n"); \
9820432001-07-01Martin Stjernholm  ) \
fc2c831999-04-01Fredrik Hübinette (Hubbe)  \ tmp=PIKE_CONCAT3(free_,DATA,s); \
f87e032000-11-25Henrik Grubbström (Grubba)  PIKE_CONCAT3(free_,DATA,s)=(struct DATA *)tmp->BLOCK_ALLOC_NEXT; \
7d63aa2001-11-08Fredrik Hübinette (Hubbe)  DO_IF_DMALLOC( \ dmalloc_unregister(tmp, 1); \ dmalloc_register(tmp,sizeof(struct DATA), DMALLOC_LOCATION()); \ ) \
fc2c831999-04-01Fredrik Hübinette (Hubbe)  INIT_BLOCK(tmp); \ return tmp; \ } \ \
b076ce2001-08-31Fredrik Hübinette (Hubbe) DO_IF_RUN_UNLOCKED( \
0f5ac12001-09-02Henrik Grubbström (Grubba) struct DATA *PIKE_CONCAT(alloc_,DATA)(void) \
805f812001-03-30Fredrik Hübinette (Hubbe) { \ struct DATA *ret; \ DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \ ret=PIKE_CONCAT3(alloc_,DATA,_unlocked)(); \ DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ return ret; \
b076ce2001-08-31Fredrik Hübinette (Hubbe) }) \
805f812001-03-30Fredrik Hübinette (Hubbe)  \
fd46f52001-09-25Fredrik Hübinette (Hubbe) DO_IF_DMALLOC( \ static void PIKE_CONCAT(check_free_,DATA)(struct DATA *d) \ { \ struct PIKE_CONCAT(DATA,_block) *tmp; \ for(tmp=PIKE_CONCAT(DATA,_blocks);tmp;tmp=tmp->next) \ { \ if( (char *)d < (char *)tmp) continue; \ if( (char *)d >= (char *)(tmp->x+BSIZE)) continue; \ return; \ } \
5aad932002-08-15Marcus Comstedt  Pike_fatal("really_free_%s called on non-block_alloc region (%p).\n", \
fd46f52001-09-25Fredrik Hübinette (Hubbe)  #DATA, d); \ } \ ) \ \
b076ce2001-08-31Fredrik Hübinette (Hubbe) DO_IF_RUN_UNLOCKED( \
805f812001-03-30Fredrik Hübinette (Hubbe) void PIKE_CONCAT3(really_free_,DATA,_unlocked)(struct DATA *d) \ { \ EXIT_BLOCK(d); \
fd46f52001-09-25Fredrik Hübinette (Hubbe)  DO_IF_DMALLOC( PIKE_CONCAT(check_free_,DATA)(d); \
08cf752001-10-06Fredrik Hübinette (Hubbe)  dmalloc_mark_as_free(d, 1); ) \
805f812001-03-30Fredrik Hübinette (Hubbe)  d->BLOCK_ALLOC_NEXT = (void *)PIKE_CONCAT3(free_,DATA,s); \ PRE_INIT_BLOCK(d); \ PIKE_CONCAT3(free_,DATA,s)=d; \
b076ce2001-08-31Fredrik Hübinette (Hubbe) }) \
805f812001-03-30Fredrik Hübinette (Hubbe)  \
fc2c831999-04-01Fredrik Hübinette (Hubbe) void PIKE_CONCAT(really_free_,DATA)(struct DATA *d) \ { \ EXIT_BLOCK(d); \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \
fd46f52001-09-25Fredrik Hübinette (Hubbe)  DO_IF_DMALLOC( PIKE_CONCAT(check_free_,DATA)(d); \
08cf752001-10-06Fredrik Hübinette (Hubbe)  dmalloc_mark_as_free(d, 1); ) \
f87e032000-11-25Henrik Grubbström (Grubba)  d->BLOCK_ALLOC_NEXT = (void *)PIKE_CONCAT3(free_,DATA,s); \
fc2c831999-04-01Fredrik Hübinette (Hubbe)  PRE_INIT_BLOCK(d); \ PIKE_CONCAT3(free_,DATA,s)=d; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \
fc2c831999-04-01Fredrik Hübinette (Hubbe) } \ \
805f812001-03-30Fredrik Hübinette (Hubbe) static void PIKE_CONCAT3(free_all_,DATA,_blocks_unlocked)(void) \
fc2c831999-04-01Fredrik Hübinette (Hubbe) { \ struct PIKE_CONCAT(DATA,_block) *tmp; \
60d9872000-03-23Fredrik Hübinette (Hubbe)  DO_IF_DMALLOC( \ for(tmp=PIKE_CONCAT(DATA,_blocks);tmp;tmp=tmp->next) \ { \ int tmp2; \
111fdd2000-04-17Fredrik Hübinette (Hubbe)  extern void dmalloc_check_block_free(void *p, char *loc); \
60d9872000-03-23Fredrik Hübinette (Hubbe)  for(tmp2=0;tmp2<BSIZE;tmp2++) \
7d63aa2001-11-08Fredrik Hübinette (Hubbe)  { \
111fdd2000-04-17Fredrik Hübinette (Hubbe)  dmalloc_check_block_free(tmp->x+tmp2, DMALLOC_LOCATION()); \
7d63aa2001-11-08Fredrik Hübinette (Hubbe)  dmalloc_unregister(tmp->x+tmp2, 1); \ } \
60d9872000-03-23Fredrik Hübinette (Hubbe)  } \ ) \
fc2c831999-04-01Fredrik Hübinette (Hubbe)  while((tmp=PIKE_CONCAT(DATA,_blocks))) \ { \ PIKE_CONCAT(DATA,_blocks)=tmp->next; \ free((char *)tmp); \ } \ PIKE_CONCAT(DATA,_blocks)=0; \ PIKE_CONCAT3(free_,DATA,s)=0; \ } \
424d9c1999-05-02Fredrik Hübinette (Hubbe)  \
805f812001-03-30Fredrik Hübinette (Hubbe) void PIKE_CONCAT3(free_all_,DATA,_blocks)(void) \ { \ DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \ PIKE_CONCAT3(free_all_,DATA,_blocks_unlocked)(); \ DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ } \
424d9c1999-05-02Fredrik Hübinette (Hubbe)  \ void PIKE_CONCAT3(count_memory_in_,DATA,s)(INT32 *num_, INT32 *size_) \ { \ INT32 num=0, size=0; \ struct PIKE_CONCAT(DATA,_block) *tmp; \ struct DATA *tmp2; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \
424d9c1999-05-02Fredrik Hübinette (Hubbe)  for(tmp=PIKE_CONCAT(DATA,_blocks);tmp;tmp=tmp->next) \ { \ num+=BSIZE; \ size+=sizeof(struct PIKE_CONCAT(DATA,_block)); \
cfa7d82000-01-27Fredrik Hübinette (Hubbe)  COUNT_BLOCK(tmp); \
424d9c1999-05-02Fredrik Hübinette (Hubbe)  } \
011ad31999-10-22Fredrik Hübinette (Hubbe)  for(tmp2=PIKE_CONCAT3(free_,DATA,s);tmp2; \
f87e032000-11-25Henrik Grubbström (Grubba)  tmp2 = (struct DATA *)tmp2->BLOCK_ALLOC_NEXT) num--; \
cfa7d82000-01-27Fredrik Hübinette (Hubbe)  COUNT_OTHER(); \
424d9c1999-05-02Fredrik Hübinette (Hubbe)  *num_=num; \ *size_=size; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ } \ \ \ void PIKE_CONCAT3(init_,DATA,_blocks)(void) \ { \
805f812001-03-30Fredrik Hübinette (Hubbe) /* DO_IF_RUN_UNLOCKED(mt_init_recursive(&PIKE_CONCAT(DATA,_mutex)));*/ \ DO_IF_RUN_UNLOCKED(mt_init(&PIKE_CONCAT(DATA,_mutex))); \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  PIKE_CONCAT3(free_,DATA,s)=0; \ } \
fc2c831999-04-01Fredrik Hübinette (Hubbe) 
afbc722001-09-06Fredrik Hübinette (Hubbe) #define LOW_PTR_HASH_ALLOC(DATA,BSIZE) \
011ad31999-10-22Fredrik Hübinette (Hubbe)  \ BLOCK_ALLOC(DATA,BSIZE) \ \
3f84322000-04-23Martin Stjernholm struct DATA **PIKE_CONCAT(DATA,_hash_table)=0; \
59e0112001-07-03Fredrik Hübinette (Hubbe) size_t PIKE_CONCAT(DATA,_hash_table_size)=0; \
afbc722001-09-06Fredrik Hübinette (Hubbe) size_t PIKE_CONCAT(DATA,_hash_table_magnitude)=0; \
59e0112001-07-03Fredrik Hübinette (Hubbe) static size_t PIKE_CONCAT(num_,DATA)=0; \
011ad31999-10-22Fredrik Hübinette (Hubbe)  \
25bdc72001-08-31Martin Stjernholm static inline struct DATA * \
59e0112001-07-03Fredrik Hübinette (Hubbe)  PIKE_CONCAT3(really_low_find_,DATA,_unlocked)(void *ptr, size_t hval) \
011ad31999-10-22Fredrik Hübinette (Hubbe) { \ struct DATA *p,**pp; \
ec6b782000-03-21Fredrik Hübinette (Hubbe)  p=PIKE_CONCAT(DATA,_hash_table)[hval]; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  if(!p || p->data == ptr) \ { \ DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ return p; \ } \
ec6b782000-03-21Fredrik Hübinette (Hubbe)  while((p=*(pp=&p->BLOCK_ALLOC_NEXT))) \
011ad31999-10-22Fredrik Hübinette (Hubbe)  { \ if(p->data==ptr) \ { \ *pp=p->BLOCK_ALLOC_NEXT; \ p->BLOCK_ALLOC_NEXT=PIKE_CONCAT(DATA,_hash_table)[hval]; \ PIKE_CONCAT(DATA,_hash_table)[hval]=p; \ return p; \ } \ } \ return 0; \ } \ \ struct DATA *PIKE_CONCAT(find_,DATA)(void *ptr) \ { \
805f812001-03-30Fredrik Hübinette (Hubbe)  struct DATA *p; \
c7241b2000-08-10Henrik Grubbström (Grubba)  size_t hval = (size_t)ptr; \
805f812001-03-30Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \ if(!PIKE_CONCAT(DATA,_hash_table_size)) { \ DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ return 0; \ } \
011ad31999-10-22Fredrik Hübinette (Hubbe)  hval%=PIKE_CONCAT(DATA,_hash_table_size); \
805f812001-03-30Fredrik Hübinette (Hubbe)  p=PIKE_CONCAT3(really_low_find_,DATA,_unlocked)(ptr, hval); \ DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ return p; \
011ad31999-10-22Fredrik Hübinette (Hubbe) } \ \ \
805f812001-03-30Fredrik Hübinette (Hubbe)  \
59e0112001-07-03Fredrik Hübinette (Hubbe) struct DATA *PIKE_CONCAT(make_,DATA)(void *ptr, size_t hval) \
805f812001-03-30Fredrik Hübinette (Hubbe) { \ struct DATA *p; \ DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \ p=PIKE_CONCAT3(make_,DATA,_unlocked)(ptr,hval); \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \
011ad31999-10-22Fredrik Hübinette (Hubbe)  return p; \ } \ \
f3378f2000-05-16Fredrik Hübinette (Hubbe) struct DATA *PIKE_CONCAT(get_,DATA)(void *ptr) \
011ad31999-10-22Fredrik Hübinette (Hubbe) { \ struct DATA *p; \
59e0112001-07-03Fredrik Hübinette (Hubbe)  size_t hval=(size_t)ptr; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \
011ad31999-10-22Fredrik Hübinette (Hubbe)  hval%=PIKE_CONCAT(DATA,_hash_table_size); \
805f812001-03-30Fredrik Hübinette (Hubbe)  if(!(p=PIKE_CONCAT3(really_low_find_,DATA,_unlocked)(ptr, hval))) \ p=PIKE_CONCAT3(make_,DATA,_unlocked)(ptr, hval); \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ return p; \
011ad31999-10-22Fredrik Hübinette (Hubbe) } \ \ int PIKE_CONCAT3(check_,DATA,_semafore)(void *ptr) \ { \ struct DATA *p; \
59e0112001-07-03Fredrik Hübinette (Hubbe)  size_t hval=(size_t)ptr; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \
011ad31999-10-22Fredrik Hübinette (Hubbe)  hval%=PIKE_CONCAT(DATA,_hash_table_size); \
805f812001-03-30Fredrik Hübinette (Hubbe)  if((p=PIKE_CONCAT3(really_low_find_,DATA,_unlocked)(ptr, hval))) \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  { \ DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \
011ad31999-10-22Fredrik Hübinette (Hubbe)  return 0; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  } \
011ad31999-10-22Fredrik Hübinette (Hubbe)  \
805f812001-03-30Fredrik Hübinette (Hubbe)  PIKE_CONCAT3(make_,DATA,_unlocked)(ptr, hval); \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \
011ad31999-10-22Fredrik Hübinette (Hubbe)  return 1; \ } \ \ int PIKE_CONCAT(remove_,DATA)(void *ptr) \ { \ struct DATA *p; \
59e0112001-07-03Fredrik Hübinette (Hubbe)  size_t hval=(size_t)ptr; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \ if(!PIKE_CONCAT(DATA,_hash_table)) \ { \ DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ return 0; \ } \
011ad31999-10-22Fredrik Hübinette (Hubbe)  hval%=PIKE_CONCAT(DATA,_hash_table_size); \
805f812001-03-30Fredrik Hübinette (Hubbe)  if((p=PIKE_CONCAT3(really_low_find_,DATA,_unlocked)(ptr, hval))) \
011ad31999-10-22Fredrik Hübinette (Hubbe)  { \ PIKE_CONCAT(num_,DATA)--; \
afbc722001-09-06Fredrik Hübinette (Hubbe)  DO_IF_DEBUG( if(PIKE_CONCAT(DATA,_hash_table)[hval]!=p) \
5aad932002-08-15Marcus Comstedt  Pike_fatal("GAOssdf\n"); ); \
011ad31999-10-22Fredrik Hübinette (Hubbe)  PIKE_CONCAT(DATA,_hash_table)[hval]=p->BLOCK_ALLOC_NEXT; \
b076ce2001-08-31Fredrik Hübinette (Hubbe)  BA_UL(PIKE_CONCAT(really_free_,DATA))(p); \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \
011ad31999-10-22Fredrik Hübinette (Hubbe)  return 1; \ } \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \
011ad31999-10-22Fredrik Hübinette (Hubbe)  return 0; \ } \ \
afbc722001-09-06Fredrik Hübinette (Hubbe) void PIKE_CONCAT3(low_init_,DATA,_hash)(size_t size) \
011ad31999-10-22Fredrik Hübinette (Hubbe) { \ extern INT32 hashprimes[32]; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  extern int my_log2(size_t x); \ PIKE_CONCAT3(init_,DATA,_blocks)(); \ DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \
afbc722001-09-06Fredrik Hübinette (Hubbe)  PIKE_CONCAT(DATA,_hash_table_size)= \ hashprimes[PIKE_CONCAT(DATA,_hash_table_magnitude)=my_log2(size)]; \
011ad31999-10-22Fredrik Hübinette (Hubbe)  \ PIKE_CONCAT(DATA,_hash_table)=(struct DATA **) \ malloc(sizeof(struct DATA *)*PIKE_CONCAT(DATA,_hash_table_size)); \ if(!PIKE_CONCAT(DATA,_hash_table)) \ { \ fprintf(stderr,"Fatal: out of memory.\n"); \ exit(17); \ } \ MEMSET(PIKE_CONCAT(DATA,_hash_table),0, \ sizeof(struct DATA *)*PIKE_CONCAT(DATA,_hash_table_size)); \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \
011ad31999-10-22Fredrik Hübinette (Hubbe) } \ \
afbc722001-09-06Fredrik Hübinette (Hubbe)  \ void PIKE_CONCAT3(init_,DATA,_hash)(void) \ { \ PIKE_CONCAT3(low_init_,DATA,_hash)(BSIZE); \ } \ \
011ad31999-10-22Fredrik Hübinette (Hubbe) void PIKE_CONCAT3(exit_,DATA,_hash)(void) \ { \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \
805f812001-03-30Fredrik Hübinette (Hubbe)  PIKE_CONCAT3(free_all_,DATA,_blocks_unlocked)(); \
011ad31999-10-22Fredrik Hübinette (Hubbe)  free(PIKE_CONCAT(DATA,_hash_table)); \ PIKE_CONCAT(DATA,_hash_table)=0; \
7bcb1d1999-12-19Fredrik Hübinette (Hubbe)  PIKE_CONCAT(num_,DATA)=0; \
aad99b2001-03-28Fredrik Hübinette (Hubbe)  DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \
424d9c1999-05-02Fredrik Hübinette (Hubbe) }
afbc722001-09-06Fredrik Hübinette (Hubbe)  #define PTR_HASH_ALLOC_FIXED(DATA,BSIZE) \ struct DATA *PIKE_CONCAT3(make_,DATA,_unlocked)(void *ptr, size_t hval); \ LOW_PTR_HASH_ALLOC(DATA,BSIZE) \ \ struct DATA *PIKE_CONCAT3(make_,DATA,_unlocked)(void *ptr, size_t hval) \ { \ struct DATA *p; \ \ DO_IF_DEBUG( if(!PIKE_CONCAT(DATA,_hash_table)) \
5aad932002-08-15Marcus Comstedt  Pike_fatal("Hash table error!\n"); ) \
afbc722001-09-06Fredrik Hübinette (Hubbe)  PIKE_CONCAT(num_,DATA)++; \ \ p=BA_UL(PIKE_CONCAT(alloc_,DATA))(); \ p->data=ptr; \ p->BLOCK_ALLOC_NEXT=PIKE_CONCAT(DATA,_hash_table)[hval]; \ PIKE_CONCAT(DATA,_hash_table)[hval]=p; \ return p; \ } \ #define PTR_HASH_ALLOC(DATA,BSIZE) \ struct DATA *PIKE_CONCAT3(make_,DATA,_unlocked)(void *ptr, size_t hval); \ LOW_PTR_HASH_ALLOC(DATA,BSIZE) \ \ static void PIKE_CONCAT(DATA,_rehash)() \ { \ /* Time to re-hash */ \ extern INT32 hashprimes[32]; \ struct DATA **old_hash; \ struct DATA *p; \ size_t hval; \ size_t e; \ \ old_hash= PIKE_CONCAT(DATA,_hash_table); \ e=PIKE_CONCAT(DATA,_hash_table_size); \ \ PIKE_CONCAT(DATA,_hash_table_magnitude)++; \ PIKE_CONCAT(DATA,_hash_table_size)= \ hashprimes[PIKE_CONCAT(DATA,_hash_table_magnitude)]; \ if((PIKE_CONCAT(DATA,_hash_table)=(struct DATA **) \ malloc(PIKE_CONCAT(DATA,_hash_table_size)* \ sizeof(struct DATA *)))) \ { \ MEMSET(PIKE_CONCAT(DATA,_hash_table),0, \ sizeof(struct DATA *)*PIKE_CONCAT(DATA,_hash_table_size)); \ while(e-- > 0) \ { \ while((p=old_hash[e])) \ { \ old_hash[e]=p->BLOCK_ALLOC_NEXT; \ hval=(size_t)(p->data); \ hval%=PIKE_CONCAT(DATA,_hash_table_size); \ p->BLOCK_ALLOC_NEXT=PIKE_CONCAT(DATA,_hash_table)[hval]; \ PIKE_CONCAT(DATA,_hash_table)[hval]=p; \ } \ } \ free((char *)old_hash); \ }else{ \ PIKE_CONCAT(DATA,_hash_table)=old_hash; \ PIKE_CONCAT(DATA,_hash_table_size)=e; \ } \ } \ \ struct DATA *PIKE_CONCAT3(make_,DATA,_unlocked)(void *ptr, size_t hval) \ { \ struct DATA *p; \ \ DO_IF_DEBUG( if(!PIKE_CONCAT(DATA,_hash_table)) \
5aad932002-08-15Marcus Comstedt  Pike_fatal("Hash table error!\n"); ) \
afbc722001-09-06Fredrik Hübinette (Hubbe)  PIKE_CONCAT(num_,DATA)++; \ \
7d63aa2001-11-08Fredrik Hübinette (Hubbe)  if(( PIKE_CONCAT(num_,DATA)>>BLOCK_ALLOC_HSIZE_SHIFT ) >= \
afbc722001-09-06Fredrik Hübinette (Hubbe)  PIKE_CONCAT(DATA,_hash_table_size)) \ { \ PIKE_CONCAT(DATA,_rehash)(); \ hval=(size_t)ptr; \ hval%=PIKE_CONCAT(DATA,_hash_table_size); \ } \ \ p=BA_UL(PIKE_CONCAT(alloc_,DATA))(); \ p->data=ptr; \ p->BLOCK_ALLOC_NEXT=PIKE_CONCAT(DATA,_hash_table)[hval]; \ PIKE_CONCAT(DATA,_hash_table)[hval]=p; \ return p; \
1f5f6a2001-09-08Henrik Grubbström (Grubba) }