~ubuntu-branches/ubuntu/warty/swig1.3/warty

« back to all changes in this revision

Viewing changes to Source/DOH/Doh/memory.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Landschoff
  • Date: 2002-03-29 01:56:07 UTC
  • Revision ID: james.westby@ubuntu.com-20020329015607-c0wt03xu8oy9ioj7
Tags: upstream-1.3.11
ImportĀ upstreamĀ versionĀ 1.3.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ----------------------------------------------------------------------------- 
 
2
 * memory.c
 
3
 *
 
4
 *     This file implements all of DOH's memory management including allocation
 
5
 *     of objects and checking of objects.
 
6
 * 
 
7
 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
 
8
 *
 
9
 * Copyright (C) 1999-2000.  The University of Chicago
 
10
 * See the file LICENSE for information on usage and redistribution.    
 
11
 * ----------------------------------------------------------------------------- */
 
12
 
 
13
static char cvsroot[] = "$Header: /cvs/projects/SWIG/Source/DOH/Doh/memory.c,v 1.23.4.1 2001/07/17 16:11:15 mkoeppe Exp $";
 
14
 
 
15
#include "dohint.h"
 
16
 
 
17
#ifndef DOH_POOL_SIZE
 
18
#define DOH_POOL_SIZE         16384
 
19
#endif
 
20
 
 
21
static int   PoolSize = DOH_POOL_SIZE;
 
22
 
 
23
DOH    *DohNone = 0;    /* The DOH None object */
 
24
 
 
25
typedef struct pool {
 
26
  DohBase       *ptr;            /* Start of pool */
 
27
  int            len;            /* Length of pool */
 
28
  int            blen;           /* Byte length of pool */
 
29
  int            current;        /* Current position for next allocation */
 
30
  struct  pool  *next;           /* Next pool */
 
31
} Pool;
 
32
 
 
33
DohBase  *FreeList = 0;          /* List of free objects */
 
34
 
 
35
static Pool    *Pools = 0;
 
36
static int      pools_initialized = 0;
 
37
 
 
38
/* ----------------------------------------------------------------------
 
39
 * CreatePool() - Create a new memory pool 
 
40
 * ---------------------------------------------------------------------- */
 
41
 
 
42
static void
 
43
CreatePool() {
 
44
  Pool *p = 0;
 
45
  p = (Pool *) DohMalloc(sizeof(Pool));
 
46
  assert(p);
 
47
  p->ptr = (DohBase *) DohMalloc(sizeof(DohBase)*PoolSize);
 
48
  assert(p->ptr);
 
49
  p->len = PoolSize;
 
50
  p->blen = PoolSize*sizeof(DohBase);
 
51
  p->current = 0;
 
52
  p->next = Pools;
 
53
  Pools = p;
 
54
}
 
55
 
 
56
/* ----------------------------------------------------------------------
 
57
 * InitPools() - Initialize the memory allocator
 
58
 * ---------------------------------------------------------------------- */
 
59
 
 
60
static void 
 
61
InitPools() {
 
62
  if (pools_initialized) return;
 
63
  CreatePool();                       /* Create initial pool */
 
64
  pools_initialized = 1;
 
65
  DohNone = NewVoid(0,0);             /* Create the None object */
 
66
  DohIntern(DohNone);
 
67
}
 
68
 
 
69
/* ----------------------------------------------------------------------
 
70
 * DohCheck()
 
71
 *
 
72
 * Returns 1 if an arbitrary pointer is a DOH object.
 
73
 * ---------------------------------------------------------------------- */
 
74
 
 
75
int 
 
76
DohCheck(const DOH *ptr) {
 
77
  Pool *p = Pools;
 
78
  register char *cptr = (char *) ptr;
 
79
  register char *pptr;
 
80
  while (p) {
 
81
    pptr = (char *) p->ptr;
 
82
    if ((cptr >= pptr) && (cptr < (pptr + p->blen))) return 1;
 
83
    /*
 
84
    pptr = (char *) p->ptr;
 
85
    if ((cptr >= pptr) && (cptr < (pptr+(p->current*sizeof(DohBase))))) return 1; */
 
86
    p = p->next;
 
87
  }
 
88
  return 0;
 
89
}
 
90
 
 
91
/* -----------------------------------------------------------------------------
 
92
 * DohIntern()
 
93
 * ----------------------------------------------------------------------------- */
 
94
 
 
95
void
 
96
DohIntern(DOH *obj) {
 
97
  DohBase *b = (DohBase *) obj;
 
98
  b->flag_intern = 1;
 
99
}
 
100
 
 
101
/* ----------------------------------------------------------------------
 
102
 * DohObjMalloc()
 
103
 *
 
104
 * Allocate memory for a new object.
 
105
 * ---------------------------------------------------------------------- */
 
106
 
 
107
DOH *
 
108
DohObjMalloc(int type, void *data) {
 
109
  DohBase *obj;
 
110
  if (!pools_initialized) InitPools();
 
111
  if (FreeList) {
 
112
    obj = FreeList;
 
113
    FreeList = (DohBase *) obj->data;
 
114
  } else {
 
115
    while (Pools->current == Pools->len) {
 
116
      PoolSize *= 2;
 
117
      CreatePool();
 
118
    }
 
119
    obj = Pools->ptr + Pools->current;
 
120
    Pools->current++;
 
121
  }
 
122
  obj->type = type;
 
123
  obj->data = data;
 
124
  obj->refcount = 1;
 
125
  obj->flag_intern = 0;
 
126
  obj->flag_marked = 0;
 
127
  return (DOH *) obj;
 
128
}
 
129
 
 
130
/* ----------------------------------------------------------------------
 
131
 * DohObjFree() - Free a DOH object
 
132
 * ---------------------------------------------------------------------- */
 
133
 
 
134
void 
 
135
DohObjFree(DOH *ptr) {
 
136
  DohBase  *b;
 
137
  b = (DohBase *) ptr;
 
138
  if (b->flag_intern) return;
 
139
  b->data = (void *) FreeList;
 
140
  b->type = 0;
 
141
  FreeList = b;
 
142
}