• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2002-2003 Hewlett-Packard Co
3 	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 
5 This file is part of libunwind.
6 
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14 
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25 
26 #ifndef mempool_h
27 #define mempool_h
28 
29 /* Memory pools provide simple memory management of fixed-size
30    objects.  Memory pools are used for two purposes:
31 
32      o To ensure a stack can be unwound even when a process
33        is out of memory.
34 
35       o To ensure a stack can be unwound at any time in a
36         multi-threaded process (e.g., even at a time when the normal
37         malloc-lock is taken, possibly by the very thread that is
38         being unwind).
39 
40 
41     To achieve the second objective, memory pools allocate memory
42     directly via mmap() system call (or an equivalent facility).
43 
44     The first objective is accomplished by reserving memory ahead of
45     time.  Since the memory requirements of stack unwinding generally
46     depends on the complexity of the procedures being unwind, there is
47     no absolute guarantee that unwinding will always work, but in
48     practice, this should not be a serious problem.  */
49 
50 #include <sys/types.h>
51 
52 #include "libunwind_i.h"
53 
54 #define sos_alloc(s)		UNWI_ARCH_OBJ(_sos_alloc)(s)
55 #define mempool_init(p,s,r)	UNWI_ARCH_OBJ(_mempool_init)(p,s,r)
56 #define mempool_alloc(p)	UNWI_ARCH_OBJ(_mempool_alloc)(p)
57 #define mempool_free(p,o)	UNWI_ARCH_OBJ(_mempool_free)(p,o)
58 
59 /* The mempool structure should be treated as an opaque object.  It's
60    declared here only to enable static allocation of mempools.  */
61 struct mempool
62   {
63     pthread_mutex_t lock;
64     size_t obj_size;		/* object size (rounded up for alignment) */
65     size_t chunk_size;		/* allocation granularity */
66     unsigned int reserve;	/* minimum (desired) size of the free-list */
67     unsigned int num_free;	/* number of objects on the free-list */
68     struct object
69       {
70 	struct object *next;
71       }
72     *free_list;
73   };
74 
75 /* Emergency allocation for one-time stuff that doesn't fit the memory
76    pool model.  A limited amount of memory is available in this
77    fashion and once allocated, there is no way to free it.  */
78 extern void *sos_alloc (size_t size);
79 
80 /* Initialize POOL for an object size of OBJECT_SIZE bytes.  RESERVE
81    is the number of objects that should be reserved for use under
82    tight memory situations.  If it is zero, mempool attempts to pick a
83    reasonable default value.  */
84 extern void mempool_init (struct mempool *pool,
85 			  size_t obj_size, size_t reserve);
86 extern void *mempool_alloc (struct mempool *pool);
87 extern void mempool_free (struct mempool *pool, void *object);
88 
89 #endif /* mempool_h */
90