pike.git
/
src
/
block_alloc.h
version
»
Context lines:
10
20
40
80
file
none
3
pike.git/src/block_alloc.h:1:
-
/* $Id: block_alloc.h,v 1.
41
2002/
08
/
15
14
:
49
:
19
marcus Exp $ */
+
/* $Id: block_alloc.h,v 1.
42
2002/
09
/
30
12
:
45
:
56
marcus Exp $ */
#undef PRE_INIT_BLOCK #undef INIT_BLOCK #undef EXIT_BLOCK #undef BLOCK_ALLOC #undef LOW_PTR_HASH_ALLOC #undef PTR_HASH_ALLOC_FIXED #undef PTR_HASH_ALLOC #undef COUNT_BLOCK #undef COUNT_OTHER #undef BLOCK_ALLOC_HSIZE_SHIFT
-
+
#undef MAX_EMPTY_BLOCKS
#define PRE_INIT_BLOCK(X) #define INIT_BLOCK(X) #define EXIT_BLOCK(X) #define COUNT_BLOCK(X) #define COUNT_OTHER() #define BLOCK_ALLOC_HSIZE_SHIFT 2
-
+
#define MAX_EMPTY_BLOCKS 4
#ifdef PIKE_RUN_UNLOCKED #include "threads.h" /* Block Alloc UnLocked */ #define BA_UL(X) PIKE_CONCAT(X,_unlocked) #define BA_STATIC static #define BA_INLINE inline #else #define BA_UL(X) X #define BA_STATIC #define BA_INLINE #endif #define BLOCK_ALLOC(DATA,BSIZE) \ \ struct PIKE_CONCAT(DATA,_block) \ { \ struct PIKE_CONCAT(DATA,_block) *next; \
-
+
struct PIKE_CONCAT(DATA,_block) *prev; \
+
struct DATA *PIKE_CONCAT3(free_,DATA,s); \
+
INT32 used; \
struct DATA x[BSIZE]; \ }; \ \ static struct PIKE_CONCAT(DATA,_block) *PIKE_CONCAT(DATA,_blocks)=0; \
-
static struct DATA *PIKE_
CONCAT3
(
free_,
DATA,
s
)=(
struct DATA
*)-1; \
+
static struct
PIKE_CONCAT(
DATA
,_block)
*PIKE_
CONCAT
(
DATA,_
free_
blocks
)=(
void
*)-1;
\
+
static
INT32
PIKE_CONCAT3(num_empty_,DATA,_blocks)=0;
\
DO_IF_RUN_UNLOCKED(static PIKE_MUTEX_T PIKE_CONCAT(DATA,_mutex);) \ \ 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) \ { \ fprintf(stderr,"Fatal: out of memory.\n"); \ exit(17); \ } \
-
n->next=PIKE_CONCAT(DATA,_blocks); \
+
if((
n->next=PIKE_CONCAT(DATA,_blocks)
)) \
+
n->next->prev=n
;
\
+
n->prev=NULL; \
+
n->used=0; \
PIKE_CONCAT(DATA,_blocks)=n; \
-
+
PIKE_CONCAT(DATA,_free_blocks)=n; \
\
-
for(e=
0
;e<BSIZE;e++) \
+
n->x[0].BLOCK_ALLOC_NEXT=NULL; \
+
PRE_INIT_BLOCK( n->x ); \
+
for(e=
1
;e<BSIZE;e++) \
{ \
-
n->x[e].BLOCK_ALLOC_NEXT=(void *)
PIKE_CONCAT3(free_,DATA,s)
; \
+
n->x[e].BLOCK_ALLOC_NEXT=(void *)
&n->x[e-1]
;
\
PRE_INIT_BLOCK( (n->x+e) ); \
-
PIKE_CONCAT3(free_,DATA,s)=n->x+e; \
+
} \
-
+
n->PIKE_CONCAT3(free_,DATA,s)=&n->x[BSIZE-1]; \
} \ \ BA_STATIC BA_INLINE struct DATA *BA_UL(PIKE_CONCAT(alloc_,DATA))(void) \ { \ struct DATA *tmp; \
-
if(!
PIKE_
CONCAT3
(
free_,
DATA,
s
)
)
\
+
struct
PIKE_
CONCAT
(DATA,
_block
)
*blk;
\
+
\
+
if(!(blk = PIKE_CONCAT(DATA,_free_blocks))) { \
PIKE_CONCAT(alloc_more_,DATA)(); \
-
+
blk = PIKE_CONCAT(DATA,_blocks); \
+
blk->used++; \
+
} \
DO_IF_DEBUG( \
-
else if (PIKE_
CONCAT3
(
free_,
DATA,
s
) == (
struct
DATA
*)-1) \
-
Pike_fatal("Block alloc not initialized.\n");
\
+
else if (PIKE_
CONCAT
(
DATA,_
free_
blocks
) == (
void
*)-1)
\
+
Pike_fatal("Block alloc not initialized.\n"); \
) \
-
+
else if(!blk->used++) \
+
--PIKE_CONCAT3(num_empty_,DATA,_blocks); \
\
-
tmp=PIKE_CONCAT3(free_,DATA,s);
\
-
PIKE_CONCAT3(free_,DATA,s)=(
struct
DATA
*)tmp->BLOCK_ALLOC_NEXT; \
+
tmp
=
blk->
PIKE_CONCAT3(free_,DATA,s); \
+
if(!(blk->
PIKE_CONCAT3(free_,DATA,s)
=
(
void
*)tmp->BLOCK_ALLOC_NEXT
)) \
+
PIKE_CONCAT(DATA,_free_blocks) = blk->prev
;
\
DO_IF_DMALLOC( \ dmalloc_unregister(tmp, 1); \ dmalloc_register(tmp,sizeof(struct DATA), DMALLOC_LOCATION()); \ ) \ INIT_BLOCK(tmp); \ return tmp; \ } \ \ DO_IF_RUN_UNLOCKED( \ struct DATA *PIKE_CONCAT(alloc_,DATA)(void) \
pike.git/src/block_alloc.h:105:
return; \ } \ Pike_fatal("really_free_%s called on non-block_alloc region (%p).\n", \ #DATA, d); \ } \ ) \ \ DO_IF_RUN_UNLOCKED( \ void PIKE_CONCAT3(really_free_,DATA,_unlocked)(struct DATA *d) \ { \
+
struct PIKE_CONCAT(DATA,_block) *blk = PIKE_CONCAT(DATA,_free_blocks); \
+
if((char *)d < (char *)blk || \
+
(char *)d >= (char *)(blk->x+BSIZE)) { \
+
blk = PIKE_CONCAT(DATA,_blocks); \
+
if((char *)d < (char *)blk || \
+
(char *)d >= (char *)(blk->x+BSIZE)) { \
+
do \
+
blk = blk->next; \
+
while((char *)d < (char *)blk || \
+
(char *)d >= (char *)(blk->x+BSIZE)); \
+
if(blk == PIKE_CONCAT(DATA,_free_blocks)) \
+
PIKE_CONCAT(DATA,_free_blocks) = blk->prev; \
+
blk->prev->next = blk->next; \
+
if(blk->next) \
+
blk->next->prev = blk->prev; \
+
blk->prev = NULL; \
+
blk->next = PIKE_CONCAT(DATA,_blocks); \
+
blk->next->prev = blk; \
+
PIKE_CONCAT(DATA,_blocks) = blk; \
+
} \
+
} \
EXIT_BLOCK(d); \ DO_IF_DMALLOC( PIKE_CONCAT(check_free_,DATA)(d); \ dmalloc_mark_as_free(d, 1); ) \
-
d->BLOCK_ALLOC_NEXT = (void *)PIKE_CONCAT3(free_,DATA,s);
\
+
d->BLOCK_ALLOC_NEXT = (void *)
blk->
PIKE_CONCAT3(free_,DATA,s); \
PRE_INIT_BLOCK(d); \
-
PIKE_CONCAT3(free_,DATA,s)=d; \
+
blk->
PIKE_CONCAT3(free_,DATA,s)=d;
\
+
if(!--blk->used
&&
\
+
++PIKE_CONCAT3(num_empty_,DATA,_blocks) > MAX_EMPTY_BLOCKS && \
+
blk != PIKE_CONCAT(DATA,_free_blocks)) { \
+
PIKE_CONCAT(DATA,_blocks) = blk->next; \
+
blk->next->prev = NULL; \
+
free(blk); \
+
--PIKE_CONCAT3(num_empty_,DATA,_blocks); \
+
} \
}) \ \ void PIKE_CONCAT(really_free_,DATA)(struct DATA *d) \ { \
-
+
struct PIKE_CONCAT(DATA,_block) *blk; \
EXIT_BLOCK(d); \ DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \
-
+
blk = PIKE_CONCAT(DATA,_free_blocks); \
+
if((char *)d < (char *)blk || \
+
(char *)d >= (char *)(blk->x+BSIZE)) { \
+
blk = PIKE_CONCAT(DATA,_blocks); \
+
if((char *)d < (char *)blk || \
+
(char *)d >= (char *)(blk->x+BSIZE)) { \
+
do \
+
blk = blk->next; \
+
while((char *)d < (char *)blk || \
+
(char *)d >= (char *)(blk->x+BSIZE)); \
+
if(blk == PIKE_CONCAT(DATA,_free_blocks)) \
+
PIKE_CONCAT(DATA,_free_blocks) = blk->prev; \
+
blk->prev->next = blk->next; \
+
if(blk->next) \
+
blk->next->prev = blk->prev; \
+
blk->prev = NULL; \
+
blk->next = PIKE_CONCAT(DATA,_blocks); \
+
blk->next->prev = blk; \
+
PIKE_CONCAT(DATA,_blocks) = blk; \
+
} \
+
} \
DO_IF_DMALLOC( PIKE_CONCAT(check_free_,DATA)(d); \ dmalloc_mark_as_free(d, 1); ) \
-
d->BLOCK_ALLOC_NEXT = (void *)PIKE_CONCAT3(free_,DATA,s);
\
+
d->BLOCK_ALLOC_NEXT = (void *)
blk->
PIKE_CONCAT3(free_,DATA,s); \
PRE_INIT_BLOCK(d); \
-
PIKE_CONCAT3(free_,DATA,s)=d; \
+
blk->
PIKE_CONCAT3(free_,DATA,s)=d;
\
+
if(!--blk->used
&&
\
+
++PIKE_CONCAT3(num_empty_,DATA,_blocks) > MAX_EMPTY_BLOCKS && \
+
blk != PIKE_CONCAT(DATA,_free_blocks)) { \
+
PIKE_CONCAT(DATA,_blocks) = blk->next; \
+
blk->next->prev = NULL; \
+
free(blk); \
+
--PIKE_CONCAT3(num_empty_,DATA,_blocks); \
+
} \
DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ } \ \ static void PIKE_CONCAT3(free_all_,DATA,_blocks_unlocked)(void) \ { \ struct PIKE_CONCAT(DATA,_block) *tmp; \ DO_IF_DMALLOC( \ for(tmp=PIKE_CONCAT(DATA,_blocks);tmp;tmp=tmp->next) \ { \ int tmp2; \
pike.git/src/block_alloc.h:146:
dmalloc_unregister(tmp->x+tmp2, 1); \ } \ } \ ) \ 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; \
+
PIKE_
CONCAT
(
DATA,_
free_
blocks
)=0;
\
+
PIKE_CONCAT3(num_empty_,DATA,_blocks)=0;
\
} \ \ 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))); \ } \ \ 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; \ DO_IF_RUN_UNLOCKED(mt_lock(&PIKE_CONCAT(DATA,_mutex))); \ for(tmp=PIKE_CONCAT(DATA,_blocks);tmp;tmp=tmp->next) \ { \
-
num+=BSIZE; \
+
size+=sizeof(struct PIKE_CONCAT(DATA,_block)); \
-
+
num+=tmp->used; \
COUNT_BLOCK(tmp); \ } \
-
for(tmp2=PIKE_CONCAT3(free_,DATA,s);tmp2; \
-
tmp2 = (struct DATA *)tmp2->BLOCK_ALLOC_NEXT) num--; \
+
COUNT_OTHER(); \ *num_=num; \ *size_=size; \ DO_IF_RUN_UNLOCKED(mt_unlock(&PIKE_CONCAT(DATA,_mutex))); \ } \ \ \ void PIKE_CONCAT3(init_,DATA,_blocks)(void) \ { \ /* DO_IF_RUN_UNLOCKED(mt_init_recursive(&PIKE_CONCAT(DATA,_mutex)));*/ \ DO_IF_RUN_UNLOCKED(mt_init(&PIKE_CONCAT(DATA,_mutex))); \
-
PIKE_
CONCAT3
(
free_,
DATA,
s
)=0;
\
+
PIKE_
CONCAT
(
DATA,_
free_
blocks
)=0; \
} \ #define LOW_PTR_HASH_ALLOC(DATA,BSIZE) \ \ BLOCK_ALLOC(DATA,BSIZE) \ \ struct DATA **PIKE_CONCAT(DATA,_hash_table)=0; \ size_t PIKE_CONCAT(DATA,_hash_table_size)=0; \