source: git/omalloc/omtTestAlloc.c @ f3398d

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