~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to dotneato/neatogen/memory.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This software may only be used by you under license from AT&T Corp.
 
3
    ("AT&T").  A copy of AT&T's Source Code Agreement is available at
 
4
    AT&T's Internet website having the URL:
 
5
    <http://www.research.att.com/sw/tools/graphviz/license/source.html>
 
6
    If you received this software without first entering into a license
 
7
    with AT&T, you have an infringing copy of this software and cannot use
 
8
    it without violating AT&T's intellectual property rights.
 
9
*/
 
10
#pragma prototyped
 
11
#include "geometry.h"
 
12
#include "mem.h"
 
13
/* #include <vmalloc.h> */
 
14
#include <stdlib.h>
 
15
#include <stdio.h>
 
16
 
 
17
#ifdef DMALLOC
 
18
#include "dmalloc.h"
 
19
#endif
 
20
 
 
21
void
 
22
freeinit(Freelist *fl, int size)
 
23
{
 
24
  
 
25
    fl -> head = NULL;
 
26
    fl -> nodesize = size;
 
27
    if (fl->blocklist != NULL) {
 
28
        Freeblock  *bp, *np;
 
29
 
 
30
        bp = fl->blocklist;
 
31
        while (bp != NULL) {
 
32
            np = bp->next;
 
33
            free (bp);
 
34
            bp = np;
 
35
        }
 
36
    }
 
37
    fl -> blocklist = NULL;
 
38
}
 
39
 
 
40
void *
 
41
myalloc(unsigned int n)
 
42
{
 
43
    void *t;
 
44
  
 
45
    if ((t=malloc(n)) == NULL) {    
 
46
      fprintf(stderr, "Insufficient memory\n");
 
47
      exit(1);
 
48
    }
 
49
    return(t);
 
50
}
 
51
 
 
52
void *
 
53
getfree(Freelist *fl)
 
54
{
 
55
    int i; 
 
56
    Freenode *t;
 
57
    Freeblock *mem;
 
58
 
 
59
    if(fl->head == NULL) {
 
60
        int  size = fl->nodesize;
 
61
        char *cp;
 
62
        mem = myalloc(sizeof(Freeblock*) + sqrt_nsites * size);
 
63
        cp = ((char*)mem) + sizeof(Freeblock*);
 
64
        for(i=0; i<sqrt_nsites; i++) {
 
65
            makefree(cp + i*size, fl);
 
66
        }
 
67
        mem->next = fl->blocklist;
 
68
        fl->blocklist = mem;
 
69
    }
 
70
    t = fl -> head;
 
71
    fl -> head = t -> nextfree;
 
72
    return((void *)t);
 
73
}
 
74
 
 
75
void
 
76
makefree(void *curr, Freelist *fl)
 
77
{
 
78
    ((Freenode*)curr) -> nextfree = fl -> head;
 
79
    fl -> head = (Freenode*)curr;
 
80
}
 
81
 
 
82
#ifdef HEAP
 
83
void *getheap(Heap *hp, int sz)
 
84
{
 
85
    char      *mem;
 
86
    Heapblock *blp;
 
87
    int       size;
 
88
 
 
89
    if (hp->left < sz) {
 
90
        if (sz > hp->size) size = sz;
 
91
        else size = hp->size;
 
92
        blp = myalloc(sizeof(Heapblock*) + size);
 
93
        hp->head = ((char*)blp) + sizeof(Heapblock*);
 
94
        hp->left = size;
 
95
        blp->next = hp->blocklist;
 
96
        hp->blocklist = blp;
 
97
    }
 
98
    
 
99
    mem = hp->head;
 
100
    hp->head = mem+sz;
 
101
    hp->left -= sz;
 
102
    return((void *)mem);
 
103
}
 
104
 
 
105
void heapinit(Heap *hp, int size)
 
106
{
 
107
    hp -> head = NULL;
 
108
    hp -> size = size;
 
109
    hp -> left = 0;
 
110
    if (hp->blocklist != NULL) {
 
111
        Heapblock  *bp, *np;
 
112
 
 
113
        bp = hp->blocklist;
 
114
        while (bp != NULL) {
 
115
            np = bp->next;
 
116
            free (bp);
 
117
            bp = np;
 
118
        }
 
119
    }
 
120
    hp -> blocklist = NULL;
 
121
}
 
122
 
 
123
#endif