summaryrefslogtreecommitdiff
path: root/lib/dns/sec/dnssafe/bmempool.c
blob: 37510a46e3c8cbc7d8f68e7368255a2fb047a32e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Copyright (C) RSA Data Security, Inc. created 1993, 1996.  This is an
   unpublished work protected as such under copyright law.  This work
   contains proprietary, confidential, and trade secret information of
   RSA Data Security, Inc.  Use, disclosure or reproduction without the
   express written authorization of RSA Data Security, Inc. is
   prohibited.
 */

#include "global.h"
#include "bsafe2.h"
#include "bmempool.h"

#define ALLOCED_LIST_SLACK 10

void B_MemoryPoolConstructor (memoryPool)
B_MemoryPool *memoryPool;
{
  T_memset ((POINTER)&memoryPool->z, 0, sizeof (memoryPool->z));
} 

void B_MemoryPoolDestructor (memoryPool)
B_MemoryPool *memoryPool;
{
  B_MemoryPoolReset (memoryPool);
  T_free ((POINTER)memoryPool->z.allocedList);
}

/* For each item on the alloced list, call the DeleteFuncion if
     there is one, otherwise zerioze and free.
   Leave the list itself allocated with all NULL entries.
 */
void B_MemoryPoolReset (memoryPool)
B_MemoryPool *memoryPool;
{
  B_ALLOCED_DATA *allocedData;
  unsigned int i;

  for (i = memoryPool->z.allocedCount,
       allocedData = memoryPool->z.allocedList;
       i-- > 0;
       ++allocedData) {
    /* Only process this entry if the data is not NULL_PTR.
     */
    if (allocedData->object != NULL_PTR) {
      if (allocedData->DeleteFunction !=
          (B_MEMORY_POOL_DELETE_FUNCTION)NULL_PTR)
        /* There is a destroy function, so call. */
        (*allocedData->DeleteFunction) (allocedData->object);
      else {
        T_memset (allocedData->object, 0, allocedData->size);
        T_free (allocedData->object);
      }
      allocedData->object = NULL_PTR;
    }
  }

  memoryPool->z.allocedCount = 0;
  /* Note that maxAllocedCount still indicates the size of the alloced list. */
}

/* On any error return, NULL_PTR is returned for the data.
   Returns 0 if successful, or BE_ALLOC if cannot alloc the memory.
 */
int B_MemoryPoolAlloc (memoryPool, data, size)
B_MemoryPool *memoryPool;
POINTER *data;
unsigned int size;
{
  if ((*data = T_malloc (size)) == NULL_PTR)
    return (BE_ALLOC);
  return (B_MemoryPoolAdoptData (memoryPool, data, size));
}

/* Use alloc to allocate the newData of length size and T_memcpy data into it.
   On any error return, NULL_PTR is returned for the newData.
   Returns 0 if successful or BE_ALLOC if cannot alloc the memory.
 */
int B_MemoryPoolAllocAndCopy (memoryPool, newData, data, size)
B_MemoryPool *memoryPool;
POINTER *newData;
POINTER data;
unsigned int size;
{
  int status;
  
  if ((status = B_MemoryPoolAlloc (memoryPool, newData, size)) != 0)
    return (status);
  
  T_memcpy (*newData, data, size);
  return (0);
}

/* Put the given data on the memory pool's alloced list.
   The size of the alloced data buffer must be passed in so that it can
     be zeroized when the object is reset (Pass in a size of zero if
     the buffer does not need to be zeroized.)
   The data is passed by reference, so that if there is an error,
     the data is zeroized and freed, and the pointer to the data is set
     to NULL_PTR.
   This routine should be used with caution - it is meant be called
     immediately after an alloc.
   No check is made as to whether the data is already on the memory pool's 
     alloced list (which would be a problem since it will get freed twice).
   Returns 0 if successful or BE_ALLOC if cannot expand the alloced list.
 */       
int B_MemoryPoolAdoptData (memoryPool, data, size)
B_MemoryPool *memoryPool;
POINTER *data;
unsigned int size;
{
  int status;
  
  if ((status = B_MemoryPoolAdoptHelper
       (memoryPool, *data, size, (B_MEMORY_POOL_DELETE_FUNCTION)NULL_PTR))
      != 0) {
    T_memset (*data, 0, size);
    T_free (*data);
    *data = NULL_PTR;
    return (status);
  }

  return (0);
}

/* Put the given object on the memory pool's alloced list.
   The size of the alloced object must be passed in so that it can
     be zeroized when the object is reset (Pass in a size of zero if
     the buffer does not need to be zeroized, especially if it
     is an object and not a data buffer.)
   The object is not passed by reference.  If there is an error,
     the calling routine should clean up the object, such as zeroizing
     and freeing.
   No check is made as to whether the object is already on the memory pool's 
     alloced list (which would be a problem since it will get freed twice).
   Returns 0 if successful or BE_ALLOC if cannot expand the alloced list.
 */       
int B_MemoryPoolAdoptHelper (memoryPool, object, size, DeleteFunction)
B_MemoryPool *memoryPool;
POINTER object;
unsigned int size;
B_MEMORY_POOL_DELETE_FUNCTION DeleteFunction;
{
  POINTER newList;
  unsigned int newMaxCount;

  if (memoryPool->z.allocedCount + 1 > memoryPool->z.maxAllocedCount) {
    /* Make extra room on the alloced list.
     */
    newMaxCount = memoryPool->z.allocedCount + ALLOCED_LIST_SLACK;
    if ((newList = T_malloc (newMaxCount * sizeof (B_ALLOCED_DATA)))
        == NULL_PTR)
      /* alloc errorm so caller should clean up the object it passed. */
      return (BE_ALLOC);
    
    /* move in new list and free old list */
    T_memcpy
      (newList, (POINTER)memoryPool->z.allocedList,
       memoryPool->z.allocedCount * sizeof (B_ALLOCED_DATA));
    T_free ((POINTER)memoryPool->z.allocedList);
    memoryPool->z.allocedList = (B_ALLOCED_DATA *)newList;
    memoryPool->z.maxAllocedCount = newMaxCount;
  }
  
  /* Put object on alloced list and increment count.
   */
  memoryPool->z.allocedList[memoryPool->z.allocedCount].object = object;
  memoryPool->z.allocedList[memoryPool->z.allocedCount].size = size;
  memoryPool->z.allocedList[memoryPool->z.allocedCount++].DeleteFunction =
    DeleteFunction;
  return (0);
}

/* 'data' points to the pointer to realloc and also is used to
     return the realloced memory.
   If data points to NULL_PTR, behaves like B_MemoryPoolAlloc.
   Find 'data' on the allocedList and realloc it to the given size,
     replacing the entry on the alloced list with the new memory.
   If it is not on the allocedList, the adopt the reallocated memory.
   If the buffer must be moved during the realloc, the old buffer is not
     zeroized (unless T_realloc does the zeroizing).
   This assumes that the (POINTER *)data is not (POINTER *)NULL_PTR.
   This assumes there is no DesroyFunction for this entry.  That is,
     you should not try to resize an object.
   On any error return, NULL_PTR is returned for the data.
   Returns 0 if successful or BE_ALLOC if cannot alloc the memory.
 */
int B_MemoryPoolRealloc (memoryPool, data, size)
B_MemoryPool *memoryPool;
POINTER *data;
unsigned int size;
{
  B_ALLOCED_DATA *allocedData;

  allocedData = B_MemoryPoolFindAllocedObject (memoryPool, *data);

  if ((*data = T_realloc (*data, size)) == NULL_PTR) {
    if (allocedData != (B_ALLOCED_DATA *)NULL_PTR)
      /* Could not reallocate, so nullify this entry. */
      allocedData->object = NULL_PTR;

    return (BE_ALLOC);
  }
  
  /* Realloc was successful.
   */
  if (allocedData == (B_ALLOCED_DATA *)NULL_PTR)
    /* The data was not in the memory pool to start with, so adopt it.
       Note that this also happens when the data is initially NULL_PTR. */
    return (B_MemoryPoolAdoptData (memoryPool, data, size));
  
  /* Replace the entry on the alloced list with the new memory.
   */
  allocedData->object = *data;
  allocedData->size = size;
  return (0);
}

/* Find the object in the alloced list, call the DeleteFunction if
     there is one, zeroize it and free it, nullifying that alloced list entry.
   The object to be freed is passed by pointer and is set to NULL_PTR to
     enforce the fact that the address no longer points to valid memory.
   This assumes that the (POINTER *)data is not (POINTER *)NULL_PTR.
   If the address is not found on the alloced list, only set the address
     to NULL_PTR.
 */
void B_MemoryPoolFree (memoryPool, object)
B_MemoryPool *memoryPool;
POINTER *object;
{
  B_ALLOCED_DATA *allocedData;
  
  if ((allocedData = B_MemoryPoolFindAllocedObject (memoryPool, *object))
      != (B_ALLOCED_DATA *)NULL_PTR) {
    if (allocedData->DeleteFunction !=
        (B_MEMORY_POOL_DELETE_FUNCTION)NULL_PTR)
      /* There is a destroy function, so call. */
      (*allocedData->DeleteFunction) (allocedData->object);
    else {
      T_memset (*object, 0, allocedData->size);
      T_free (*object);
    }

    /* Set this entry to NULL_PTR so that reset will not process it. */
    allocedData->object = NULL_PTR;
  }
  
  *object = NULL_PTR;
}

/* Return a pointer to the alloced object entry in the memoryPool.
   Return (ALLOCED_DATA *)NULL_PTR if object is NULL_PTR or object is not
     in the memoryPool.
 */
B_ALLOCED_DATA *B_MemoryPoolFindAllocedObject (memoryPool, object)
B_MemoryPool *memoryPool;
POINTER object;
{
  B_ALLOCED_DATA *allocedData;
  unsigned int i;
  
  if (object == NULL_PTR)
    return ((B_ALLOCED_DATA *)NULL_PTR);
  
  for (i = memoryPool->z.allocedCount,
       allocedData = memoryPool->z.allocedList;
       i-- > 0;
       ++allocedData) {
    if (allocedData->object == object)
      return (allocedData);
  }

  /* data not found. */
  return ((B_ALLOCED_DATA *)NULL_PTR);
}