source: git/omalloc/omtTestAlloc.c @ 4697a8a

fieker-DuValspielwiese
Last change on this file since 4697a8a was e70e45, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* re-added files git-svn-id: file:///usr/local/Singular/svn/trunk@4521 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.1 KB
Line 
1/*******************************************************************
2 *  File:    omtTestAlloc.c
3 *  Purpose: alloc function to be included in omMain.c
4 *  Author:  obachman@mathematik.uni-kl.de (Olaf Bachmann)
5 *  Created: 11/99
6 *  Version: $Id: omtTestAlloc.c,v 1.3 2000-08-14 12:26:52 obachman Exp $
7 *******************************************************************/
8#include "omtTest.h"
9
10#ifdef TEST_CHECK
11#define OM_CHECK CHECK_LEVEL
12#define omtTestAlloc   omtTestAllocDebug
13#define omtTestRealloc omtTestReallocDebug
14#define omtTestFree    omtTestFreeDebug
15#define omtTestDup     omtTestDupDebug
16#endif
17
18#include "omalloc.h"
19
20#ifndef OM_ALIGNMENT_NEEDS_WORK
21#define omSmallSize2AlignedBin omSmallSize2Bin
22#endif
23
24void omtTestAlloc(omMemCell cell, int spec)
25{
26  int size = GET_SIZE(spec);
27  void* addr;
28  omBin bin = NULL;
29
30  if (IS_BIN(spec) && (size <= OM_MAX_BLOCK_SIZE || IS_SPEC_BIN(spec)))
31  {
32    if (IS_SPEC_BIN(spec))
33    {
34      if (IS_ALIGNED(spec))
35        bin = omGetAlignedSpecBin(size);
36      else
37        bin = omGetSpecBin(size); 
38    }
39    else
40    {
41      if (IS_ALIGNED(spec))
42        bin = omSmallSize2AlignedBin(size);
43      else
44        bin = omSmallSize2Bin(size);
45    }
46   
47    if (IS_INLINE(spec))
48    {
49      if (IS_ZERO(spec))
50        addr = omAlloc0Bin(bin);
51      else
52        addr = omAllocBin(bin);
53    }
54    else
55    {
56      if (IS_ZERO(spec))
57        omTypeAlloc0Bin(void*, addr, bin);
58      else
59        omTypeAllocBin(void*, addr, bin);
60    }
61  }
62  else
63  {
64    if (IS_INLINE(spec))
65    {
66      if (IS_ZERO(spec))
67      {
68        if (IS_ALIGNED(spec))
69        {
70          if (IS_SLOPPY(spec))
71            addr = omalloc0(size);
72          else
73            addr = omAlloc0Aligned(size);
74        }
75        else
76          addr = omAlloc0(size);
77      }
78      else
79      {
80        if (IS_ALIGNED(spec))
81        {
82          if (IS_SLOPPY(spec))
83            addr = omalloc(size);
84          else
85            addr = omAllocAligned(size);
86        }
87        else
88          addr = omAlloc(size);
89      }
90    }
91    else
92    {
93      if (IS_ZERO(spec))
94      {
95        if (IS_ALIGNED(spec))
96          omTypeAlloc0Aligned(void*, addr, size);
97        else
98          omTypeAlloc0(void*, addr, size);
99      }
100      else
101      {
102        if (IS_ALIGNED(spec))
103          omTypeAllocAligned(void*, addr, size);
104        else
105          omTypeAlloc(void*, addr, size);
106      }
107    }
108  }
109  cell->addr = addr;
110  cell->bin = bin;
111  cell->spec = spec;
112
113  InitCellAddrContent(cell);
114
115  omtTestDebug(cell);
116}
117
118void omtTestFree(omMemCell cell)
119{
120  void* addr = cell->addr;
121  int spec = cell->spec;
122  omBin bin = cell->bin;
123  size_t size = GET_SIZE(spec);
124
125  omtTestDebug(cell);
126
127  if (IS_FREE_SIZE(spec))
128  {
129    if (IS_SLOPPY(spec))
130      omfreeSize(addr, size);
131    else
132      omFreeSize(addr, size);
133  }
134  else if (bin != NULL && IS_FREE_BIN(spec))
135    omFreeBin(addr, bin);
136  else
137  {
138    if (IS_SLOPPY(spec))
139      omfree(addr);
140    else
141      omFree(addr);
142  }
143 
144  if (bin != NULL && IS_SPEC_BIN(spec))
145    omUnGetSpecBin(&bin);
146
147  cell->addr = NULL;
148  cell->spec = 0;
149  cell->bin = NULL;
150}
151
152void omtTestRealloc(omMemCell cell, int new_spec)
153{
154  void* old_addr = cell->addr;
155  int old_spec = cell->spec;
156  omBin old_bin = cell->bin;
157  size_t old_size = GET_SIZE(old_spec);
158  void* new_addr;
159  omBin new_bin = NULL;
160  size_t new_size = GET_SIZE(new_spec);
161  size_t real_old_size = omSizeOfAddr(old_addr);
162  size_t min_size;
163 
164  omtTestDebug(cell);
165
166  if (old_bin != NULL && IS_FREE_BIN(old_spec) && 
167      IS_BIN(new_spec) && ((new_size <= OM_MAX_BLOCK_SIZE) || IS_SPEC_BIN(new_spec)))
168  {
169    if (IS_SPEC_BIN(new_spec))
170    {
171      if (IS_ALIGNED(new_spec))
172        new_bin = omGetAlignedSpecBin(new_size);
173      else
174        new_bin = omGetSpecBin(new_size); 
175    }
176    else
177    {
178      if (IS_ALIGNED(new_spec))
179        new_bin = omSmallSize2AlignedBin(new_size);
180      else
181        new_bin = omSmallSize2Bin(new_size);
182    } 
183       
184    if (IS_INLINE(new_spec))
185    {
186      if (IS_ZERO(new_spec)) new_addr = omRealloc0Bin(old_addr, old_bin, new_bin);
187      else new_addr = omReallocBin(old_addr, old_bin, new_bin);
188    }
189    else
190    {
191      if (IS_ZERO(new_spec)) omTypeRealloc0Bin(old_addr, old_bin, void*, new_addr, new_bin);
192      else omTypeReallocBin(old_addr, old_bin, void*, new_addr, new_bin);
193    }
194  }
195  else
196  {
197    if (IS_FREE_SIZE(old_spec))
198    {
199      if (IS_INLINE(new_spec))
200      {
201        if (IS_ZERO(new_spec))
202        {
203          if (IS_ALIGNED(new_spec)) 
204          {
205            if (IS_SLOPPY(new_spec))
206              new_addr = omrealloc0Size(old_addr, old_size, new_size);
207            else
208              new_addr = omRealloc0AlignedSize(old_addr, old_size, new_size);
209          }
210          else
211            new_addr = omRealloc0Size(old_addr, old_size, new_size);
212        }
213        else
214        {
215          if (IS_ALIGNED(new_spec)) 
216          {
217            if (IS_SLOPPY(new_spec))
218              new_addr = omreallocSize(old_addr, old_size, new_size);
219            else
220              new_addr = omReallocAlignedSize(old_addr, old_size, new_size);
221          }
222          else  new_addr = omReallocSize(old_addr, old_size, new_size);
223        }
224      }
225      else
226      {
227        if (IS_ZERO(new_spec))
228        {
229          if (IS_ALIGNED(new_spec)) omTypeRealloc0AlignedSize(old_addr, old_size, void*, new_addr, new_size);
230          else  omTypeRealloc0Size(old_addr, old_size, void*, new_addr, new_size);
231        }
232        else
233        {
234          if (IS_ALIGNED(new_spec))  omTypeReallocAlignedSize(old_addr, old_size, void*, new_addr, new_size);
235          else  omTypeReallocSize(old_addr, old_size, void*, new_addr, new_size);
236        }
237      }
238    }
239    else
240    {
241      if (IS_INLINE(new_spec))
242      {
243        if (IS_ZERO(new_spec))
244        {
245          if (IS_ALIGNED(new_spec)) 
246          {
247            if (IS_SLOPPY(new_spec))
248              new_addr = omrealloc0(old_addr, new_size);
249            else
250              new_addr = omRealloc0Aligned(old_addr, new_size);
251          }
252          else  new_addr = omRealloc0(old_addr, new_size);
253        }
254        else
255        {
256          if (IS_ALIGNED(new_spec)) 
257          {
258            if (IS_SLOPPY(new_spec))
259              new_addr = omrealloc(old_addr, new_size);
260            else
261              new_addr = omReallocAligned(old_addr, new_size);
262          }
263          else  new_addr = omRealloc(old_addr, new_size);
264        }
265      }
266      else
267      {
268        if (IS_ZERO(new_spec))
269        {
270          if (IS_ALIGNED(new_spec)) omTypeRealloc0Aligned(old_addr, void*, new_addr, new_size);
271          else  omTypeRealloc0(old_addr, void*, new_addr, new_size);
272        }
273        else
274        {
275          if (IS_ALIGNED(new_spec))  omTypeReallocAligned(old_addr, void*, new_addr, new_size);
276          else  omTypeRealloc(old_addr, void*, new_addr, new_size);
277        }
278      }
279    }
280  }
281
282  if (old_bin != NULL && IS_SPEC_BIN(old_spec)) 
283      omUnGetSpecBin(&old_bin);
284
285  new_size = omSizeOfAddr(new_addr);
286  old_size = real_old_size;
287  min_size = (new_size < old_size ? new_size : old_size);
288 
289  if (IS_ZERO(old_spec) && IS_ZERO(new_spec))
290    TestAddrContent(new_addr, 0, new_size);
291  else
292  {
293    TestAddrContent(new_addr, (IS_ZERO(old_spec) ? 0 : old_spec), min_size);
294    if (IS_ZERO(new_spec) &&  old_size < new_size)
295      TestAddrContent(new_addr + old_size, 0, new_size - old_size);
296  }
297
298  cell->addr = new_addr;
299  cell->spec = new_spec;
300  cell->bin = new_bin;
301  InitCellAddrContent(cell);
302  omtTestDebug(cell);
303}
304
305#define DO_STRDUP(l) (l & 1)
306void omtTestDup(omMemCell cell, int spec)
307{
308  omtTestDebug(cell);
309 
310  if (DO_STRDUP(spec))
311  {
312    size_t size = omSizeOfAddr(cell->addr);
313    void* new_addr;
314    memset(cell->addr, 'a', size - 1);
315    ((char*) cell->addr)[size-1] = '\0';
316    new_addr = omStrDup(cell->addr);
317    TestAddrContentEqual(new_addr, cell->addr, size);
318    omFree(new_addr);
319    InitCellAddrContent(cell);
320  }
321  else
322  {
323    void* new_addr = omMemDup(cell->addr);
324    TestAddrContentEqual(new_addr, cell->addr, omSizeOfAddr(cell->addr));
325    omFree(new_addr);
326    new_addr = omMemDupAligned(cell->addr);
327    TestAddrContentEqual(new_addr, cell->addr, omSizeOfAddr(cell->addr));
328    omDebugAddrAlignedSize(new_addr,  omSizeOfAddr(cell->addr));
329    omFree(new_addr);
330  }
331}
332
333   
Note: See TracBrowser for help on using the repository browser.