1 |
4 |
atypic |
/*
|
2 |
|
|
* Copyright (c) 2004, Swedish Institute of Computer Science.
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions
|
7 |
|
|
* are met:
|
8 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
9 |
|
|
* notice, this list of conditions and the following disclaimer.
|
10 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
11 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
12 |
|
|
* documentation and/or other materials provided with the distribution.
|
13 |
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
14 |
|
|
* may be used to endorse or promote products derived from this software
|
15 |
|
|
* without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
18 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
19 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
21 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
23 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
24 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
26 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
27 |
|
|
* SUCH DAMAGE.
|
28 |
|
|
*
|
29 |
|
|
* This file is part of the uIP TCP/IP stack
|
30 |
|
|
*
|
31 |
|
|
* Author: Adam Dunkels <adam@sics.se>
|
32 |
|
|
*
|
33 |
|
|
* $Id: memb.h,v 1.1 2006/06/12 08:21:43 adam Exp $
|
34 |
|
|
*/
|
35 |
|
|
|
36 |
|
|
/**
|
37 |
|
|
* \defgroup memb Memory block management functions
|
38 |
|
|
*
|
39 |
|
|
* The memory block allocation routines provide a simple yet powerful
|
40 |
|
|
* set of functions for managing a set of memory blocks of fixed
|
41 |
|
|
* size. A set of memory blocks is statically declared with the
|
42 |
|
|
* MEMB() macro. Memory blocks are allocated from the declared
|
43 |
|
|
* memory by the memb_alloc() function, and are deallocated with the
|
44 |
|
|
* memb_free() function.
|
45 |
|
|
*
|
46 |
|
|
* \note Because of namespace clashes only one MEMB() can be
|
47 |
|
|
* declared per C module, and the name scope of a MEMB() memory
|
48 |
|
|
* block is local to each C module.
|
49 |
|
|
*
|
50 |
|
|
* The following example shows how to declare and use a memory block
|
51 |
|
|
* called "cmem" which has 8 chunks of memory with each memory chunk
|
52 |
|
|
* being 20 bytes large.
|
53 |
|
|
*
|
54 |
|
|
* @{
|
55 |
|
|
*/
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
|
|
* \file
|
60 |
|
|
* Memory block allocation routines.
|
61 |
|
|
* \author
|
62 |
|
|
* Adam Dunkels <adam@sics.se>
|
63 |
|
|
*
|
64 |
|
|
*/
|
65 |
|
|
|
66 |
|
|
#ifndef __MEMB_H__
|
67 |
|
|
#define __MEMB_H__
|
68 |
|
|
|
69 |
|
|
/*
|
70 |
|
|
* Here we define a C preprocessing macro for concatenating to
|
71 |
|
|
* strings. We need use two macros in order to allow concatenation of
|
72 |
|
|
* two #defined macros.
|
73 |
|
|
*/
|
74 |
|
|
#define MEMB_CONCAT2(s1, s2) s1##s2
|
75 |
|
|
#define MEMB_CONCAT(s1, s2) MEMB_CONCAT2(s1, s2)
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* Declare a memory block.
|
79 |
|
|
*
|
80 |
|
|
* This macro is used to staticall declare a block of memory that can
|
81 |
|
|
* be used by the block allocation functions. The macro statically
|
82 |
|
|
* declares a C array with a size that matches the specified number of
|
83 |
|
|
* blocks and their individual sizes.
|
84 |
|
|
*
|
85 |
|
|
* Example:
|
86 |
|
|
\code
|
87 |
|
|
MEMB(connections, sizeof(struct connection), 16);
|
88 |
|
|
\endcode
|
89 |
|
|
*
|
90 |
|
|
* \param name The name of the memory block (later used with
|
91 |
|
|
* memb_init(), memb_alloc() and memb_free()).
|
92 |
|
|
*
|
93 |
|
|
* \param size The size of each memory chunk, in bytes.
|
94 |
|
|
*
|
95 |
|
|
* \param num The total number of memory chunks in the block.
|
96 |
|
|
*
|
97 |
|
|
*/
|
98 |
|
|
#define MEMB(name, structure, num) \
|
99 |
|
|
static char MEMB_CONCAT(name,_memb_count)[num]; \
|
100 |
|
|
static structure MEMB_CONCAT(name,_memb_mem)[num]; \
|
101 |
|
|
static struct memb_blocks name = {sizeof(structure), num, \
|
102 |
|
|
MEMB_CONCAT(name,_memb_count), \
|
103 |
|
|
(void *)MEMB_CONCAT(name,_memb_mem)}
|
104 |
|
|
|
105 |
|
|
struct memb_blocks {
|
106 |
|
|
unsigned short size;
|
107 |
|
|
unsigned short num;
|
108 |
|
|
char *count;
|
109 |
|
|
void *mem;
|
110 |
|
|
};
|
111 |
|
|
|
112 |
|
|
/**
|
113 |
|
|
* Initialize a memory block that was declared with MEMB().
|
114 |
|
|
*
|
115 |
|
|
* \param m A memory block previosly declared with MEMB().
|
116 |
|
|
*/
|
117 |
|
|
void memb_init(struct memb_blocks *m);
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
* Allocate a memory block from a block of memory declared with MEMB().
|
121 |
|
|
*
|
122 |
|
|
* \param m A memory block previosly declared with MEMB().
|
123 |
|
|
*/
|
124 |
|
|
void *memb_alloc(struct memb_blocks *m);
|
125 |
|
|
|
126 |
|
|
/**
|
127 |
|
|
* Deallocate a memory block from a memory block previously declared
|
128 |
|
|
* with MEMB().
|
129 |
|
|
*
|
130 |
|
|
* \param m m A memory block previosly declared with MEMB().
|
131 |
|
|
*
|
132 |
|
|
* \param ptr A pointer to the memory block that is to be deallocated.
|
133 |
|
|
*
|
134 |
|
|
* \return The new reference count for the memory block (should be 0
|
135 |
|
|
* if successfully deallocated) or -1 if the pointer "ptr" did not
|
136 |
|
|
* point to a legal memory block.
|
137 |
|
|
*/
|
138 |
|
|
char memb_free(struct memb_blocks *m, void *ptr);
|
139 |
|
|
|
140 |
|
|
/** @} */
|
141 |
|
|
|
142 |
|
|
#endif /* __MEMB_H__ */
|