/[dynamips]/upstream/dynamips-0.2.6-RC1/mempool.h
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/dynamips-0.2.6-RC1/mempool.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Sat Oct 6 16:03:58 2007 UTC (11 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 2718 byte(s)
import dynamips-0.2.6-RC1

1 /*
2 * Copyright (c) 1999-2006 Christophe Fillot.
3 * E-mail: cf@utc.fr
4 *
5 * mempool.h: Simple Memory Pools.
6 */
7
8 #ifndef __MEMPOOL_H__
9 #define __MEMPOOL_H__ 1
10
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include <pthread.h>
14
15 #include "utils.h"
16
17 /* Memory Pool "Fixed" Flag */
18 #define MEMPOOL_FIXED 1
19
20 /* Dummy value used to check if a memory block is invalid */
21 #define MEMBLOCK_TAG 0xdeadbeef
22
23 typedef struct memblock memblock_t;
24 typedef struct mempool mempool_t;
25
26 /* Memory block */
27 struct memblock {
28 int tag; /* MEMBLOCK_TAG if block is valid */
29 size_t block_size; /* Block size (without header) */
30 memblock_t *next,*prev; /* Double linked list pointers */
31 mempool_t *pool; /* Pool which contains this block */
32 m_uint64_t data[0]; /* Memory block itself */
33 };
34
35 /* Memory Pool */
36 struct mempool {
37 memblock_t *block_list; /* Double-linked block list */
38 pthread_mutex_t lock; /* Mutex for managing pool */
39 char *name; /* Name of this pool */
40 int flags; /* Flags */
41 int nr_blocks; /* Number of blocks in this pool */
42 size_t total_size; /* Total bytes allocated */
43 size_t max_size; /* Maximum memory */
44 };
45
46 /* Lock and unlock access to a memory pool */
47 #define MEMPOOL_LOCK(mp) pthread_mutex_lock(&(mp)->lock)
48 #define MEMPOOL_UNLOCK(mp) pthread_mutex_unlock(&(mp)->lock)
49
50 /* Callback function for use with mp_foreach */
51 typedef void (*mp_foreach_cbk)(memblock_t *block,void *user_arg);
52
53 /* Execute an action for each block in specified pool */
54 static inline void mp_foreach(mempool_t *pool,mp_foreach_cbk cbk,void *arg)
55 {
56 memblock_t *mb;
57
58 for(mb=pool->block_list;mb;mb=mb->next)
59 cbk(mb,arg);
60 }
61
62 /* Allocate a new block in specified pool */
63 void *mp_alloc(mempool_t *pool,size_t size);
64
65 /* Allocate a new block which will not be zeroed */
66 void *mp_alloc_n0(mempool_t *pool,size_t size);
67
68 /* Reallocate a block */
69 void *mp_realloc(void *addr,size_t new_size);
70
71 /* Allocate a new memory block and copy data into it */
72 void *mp_dup(mempool_t *pool,void *data,size_t size);
73
74 /* Duplicate specified string and insert it in a memory pool */
75 char *mp_strdup(mempool_t *pool,char *str);
76
77 /* Free block at specified address */
78 int mp_free(void *addr);
79
80 /* Free block at specified address and clean pointer */
81 int mp_free_ptr(void *addr);
82
83 /* Free all blocks of specified pool */
84 void mp_free_all_blocks(mempool_t *pool);
85
86 /* Free specified memory pool */
87 void mp_free_pool(mempool_t *pool);
88
89 /* Create a new pool in a fixed memory area */
90 mempool_t *mp_create_fixed_pool(mempool_t *mp,char *name);
91
92 /* Create a new pool */
93 mempool_t *mp_create_pool(char *name);
94
95 #endif

  ViewVC Help
Powered by ViewVC 1.1.26