source: git/factory/mmheap.c @ fbb0173

spielwiese
Last change on this file since fbb0173 was 341696, checked in by Hans Schönemann <hannes@…>, 14 years ago
Adding Id property to all files git-svn-id: file:///usr/local/Singular/svn/trunk@12231 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 2.7 KB
Line 
1/* emacs edit mode for this file is -*- C -*- */
2/* $Id$ */
3
4#define _POSIX_SOURCE 1
5
6#include <config.h>
7
8#include <stdlib.h>
9
10#include "memman.h"
11#include "mmprivate.h"
12
13static char * mm_theHeapPtr = NULL;
14static char * mm_tmpHeapPtr = NULL;
15static char * mm_normHeapPtr = NULL;
16
17static int mm_theBytesLeft = 0;
18static int mm_tmpBytesLeft = 0;
19static int mm_normBytesLeft = 0;
20
21static status_t mm_heapStatus = MM_NORMAL;
22
23static void mmDistributeRestOfHeap( void );
24
25void
26mmInitHeap( void )
27{
28    mm_theHeapPtr = mm_tmpHeapPtr = mm_normHeapPtr = NULL;
29    mm_theBytesLeft = mm_tmpBytesLeft = mm_normBytesLeft = 0;
30    mm_heapStatus = MM_NORMAL;
31}
32
33void
34mmMarkHeap( void )
35{
36    mm_normHeapPtr = mm_theHeapPtr; mm_normBytesLeft = mm_theBytesLeft;
37    mm_theHeapPtr = NULL; mm_theBytesLeft = 0;
38    mm_heapStatus = MM_TMP;
39}
40
41void
42mmSweepHeap( void )
43{
44    mm_theHeapPtr = mm_normHeapPtr; mm_theBytesLeft = mm_normBytesLeft;
45    mm_heapStatus = MM_NORMAL;
46}
47
48void
49mmSwitchHeap( void )
50{
51    if ( mm_heapStatus == MM_TMP ) {
52        mm_heapStatus = MM_SWITCHED;
53        mm_tmpHeapPtr = mm_theHeapPtr; mm_tmpBytesLeft = mm_theBytesLeft;
54        mm_theHeapPtr = mm_normHeapPtr; mm_theBytesLeft = mm_normBytesLeft;
55    }
56    else {
57        mm_heapStatus = MM_TMP;
58        mm_normHeapPtr = mm_theHeapPtr; mm_normBytesLeft = mm_theBytesLeft;
59        mm_theHeapPtr = mm_tmpHeapPtr; mm_theBytesLeft = mm_tmpBytesLeft;
60    }
61}
62
63void *
64mmGetMemory( size_t size )
65{
66    void * dummy;
67
68    if ( mm_theBytesLeft < size ) {
69        mmDistributeRestOfHeap();
70        mm_theHeapPtr = (char*)mmGetBlock();
71        mm_theBytesLeft = MAXDATA;
72    }
73    dummy = (void*)mm_theHeapPtr;
74    mm_theBytesLeft -= size;
75    mm_theHeapPtr += size;
76    return dummy;
77}
78
79int
80mmPutMemory( void *adr, size_t size )
81{
82    void * dummy=(void *)((char *)adr+size);
83
84    if ( dummy == mm_theHeapPtr) {
85        mm_theHeapPtr=(char *)adr;
86        mm_theBytesLeft += size;
87        return 1; /*TRUE*/
88    }
89    return 0; /*FALSE*/
90}
91
92#ifndef MDEBUG
93
94void
95mmDistributeRestOfHeap( void )
96{
97/*
98  char* dummy;
99  int j;
100
101  while ( mm_theBytesLeft > RealSizeFromSize( mmGetSize( 0 ) ) ) {
102  j = mmGetIndex( SizeFromRealSize( mm_theBytesLeft ) );
103  if ( RealSizeFromSize( mmGetSize( j ) ) > mm_theBytesLeft ) j--;
104  dummy = mm_theHeapPtr;
105  mm_theHeapPtr += RealSizeFromSize( mmGetSize( j ) );
106  mm_theBytesLeft -= RealSizeFromSize( mmGetSize( j ) );
107  mmFreeBlock( &(dummy[DebugOffsetFront]), mmGetSize( j ) );
108  }
109  */
110}
111
112#else /* MDEBUG */
113
114void
115mmDistributeRestOfHeap( void )
116{
117/*
118  void* dummy;
119  int j;
120
121  while ( mm_theBytesLeft > RealSizeFromSize( mmGetSize( 0 ) ) ) {
122  j = mmGetIndex( SizeFromRealSize( mm_theBytesLeft ) );
123  if ( RealSizeFromSize( mmGetSize( j ) ) > mm_theBytesLeft ) j--;
124  dummy = mmAllocBlock( mmGetSize( j ) );
125  mmFreeBlock( &dummy, mmGetSize( j ) );
126  }
127  */
128}
129
130#endif /* MDEBUG */
Note: See TracBrowser for help on using the repository browser.