dev/c/malloc_speed_test/pool1.c

65 lines
848 B
C

#include <stdlib.h>
#define MAX_I 0x00ffffff
#define bool char
#define TRUE 1
#define FALSE 0
struct object_s {
char val;
};
struct block_s {
bool busy;
struct object_s obj;
};
struct block_s pool[MAX_I];
void init()
{
size_t i;
for (i = 0; i < MAX_I; i++)
pool[i].busy = TRUE;
}
#define MAX_B (pool + MAX_I * sizeof(struct block_s))
inline void *alloc()
{
static struct block_s *b = pool;
struct block_s *old_b = b;
for ( ; b < MAX_B; b++)
if (!b->busy) {
b->busy = TRUE;
return &b->obj;
}
for (b = pool; b < old_b; b++)
if (!b->busy) {
b->busy = TRUE;
return &b->obj;
}
return NULL;
}
inline void free(void *p)
{
bool *b = (bool *)(p - sizeof(bool));
*b = FALSE;
}
int main()
{
unsigned long long i;
for (i = 0; i < MAX_I; i++)
alloc();
return 0;
}