1 | /* $Id$ $Revision$ */ |
---|---|
2 | /* vim:set shiftwidth=4 ts=8: */ |
3 | |
4 | /************************************************************************* |
5 | * Copyright (c) 2011 AT&T Intellectual Property |
6 | * All rights reserved. This program and the accompanying materials |
7 | * are made available under the terms of the Eclipse Public License v1.0 |
8 | * which accompanies this distribution, and is available at |
9 | * http://www.eclipse.org/legal/epl-v10.html |
10 | * |
11 | * Contributors: See CVS logs. Details at http://www.graphviz.org/ |
12 | *************************************************************************/ |
13 | |
14 | #include "geometry.h" |
15 | #include "render.h" |
16 | |
17 | typedef struct freenode { |
18 | struct freenode *nextfree; |
19 | } Freenode; |
20 | |
21 | typedef struct freeblock { |
22 | struct freeblock *next; |
23 | struct freenode *nodes; |
24 | } Freeblock; |
25 | |
26 | #include "mem.h" |
27 | #include <stdlib.h> |
28 | #include <stdio.h> |
29 | |
30 | static int gcd(int y, int x) |
31 | { |
32 | while (x != y) { |
33 | if (y < x) |
34 | x = x - y; |
35 | else |
36 | y = y - x; |
37 | } |
38 | return x; |
39 | } |
40 | |
41 | #define LCM(x,y) ((x)%(y) == 0 ? (x) : (y)%(x) == 0 ? (y) : x*(y/gcd(x,y))) |
42 | |
43 | void freeinit(Freelist * fl, int size) |
44 | { |
45 | |
46 | fl->head = NULL; |
47 | fl->nodesize = LCM(size, sizeof(Freenode)); |
48 | if (fl->blocklist != NULL) { |
49 | Freeblock *bp, *np; |
50 | |
51 | bp = fl->blocklist; |
52 | while (bp != NULL) { |
53 | np = bp->next; |
54 | free(bp->nodes); |
55 | free(bp); |
56 | bp = np; |
57 | } |
58 | } |
59 | fl->blocklist = NULL; |
60 | } |
61 | |
62 | void *getfree(Freelist * fl) |
63 | { |
64 | int i; |
65 | Freenode *t; |
66 | Freeblock *mem; |
67 | |
68 | if (fl->head == NULL) { |
69 | int size = fl->nodesize; |
70 | char *cp; |
71 | |
72 | mem = GNEW(Freeblock); |
73 | mem->nodes = gmalloc(sqrt_nsites * size); |
74 | cp = (char *) (mem->nodes); |
75 | for (i = 0; i < sqrt_nsites; i++) { |
76 | makefree(cp + i * size, fl); |
77 | } |
78 | mem->next = fl->blocklist; |
79 | fl->blocklist = mem; |
80 | } |
81 | t = fl->head; |
82 | fl->head = t->nextfree; |
83 | return ((void *) t); |
84 | } |
85 | |
86 | void makefree(void *curr, Freelist * fl) |
87 | { |
88 | ((Freenode *) curr)->nextfree = fl->head; |
89 | fl->head = (Freenode *) curr; |
90 | } |
91 |