Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
memory.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include <neatogen/geometry.h>
12#include <common/render.h>
13#include <util/alloc.h>
14
15typedef struct freenode {
18
19typedef struct freeblock {
20 struct freeblock *next;
21 struct freenode *nodes;
23
24#include <neatogen/mem.h>
25#include <stdlib.h>
26#include <stdio.h>
27
28static int gcd(int y, int x)
29{
30 while (x != y) {
31 if (y < x)
32 x = x - y;
33 else
34 y = y - x;
35 }
36 return x;
37}
38
39#define LCM(x,y) ((x)%(y) == 0 ? (x) : (y)%(x) == 0 ? (y) : x*(y/gcd(x,y)))
40
41void freeinit(Freelist * fl, int size)
42{
43
44 fl->head = NULL;
45 fl->nodesize = LCM(size, sizeof(Freenode));
46 if (fl->blocklist != NULL) {
47 Freeblock *bp, *np;
48
49 bp = fl->blocklist;
50 while (bp != NULL) {
51 np = bp->next;
52 free(bp->nodes);
53 free(bp);
54 bp = np;
55 }
56 }
57 fl->blocklist = NULL;
58}
59
60void *getfree(Freelist * fl)
61{
62 int i;
63 Freenode *t;
64
65 if (fl->head == NULL) {
66 int size = fl->nodesize;
67 char *cp;
68
69 Freeblock *mem = gv_alloc(sizeof(Freeblock));
70 mem->nodes = gv_calloc(sqrt_nsites, size);
71 cp = (char *) (mem->nodes);
72 for (i = 0; i < sqrt_nsites; i++) {
73 makefree(cp + i * size, fl);
74 }
75 mem->next = fl->blocklist;
76 fl->blocklist = mem;
77 }
78 t = fl->head;
79 fl->head = t->nextfree;
80 return t;
81}
82
83void makefree(void *curr, Freelist * fl)
84{
85 ((Freenode *) curr)->nextfree = fl->head;
86 fl->head = curr;
87}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
int sqrt_nsites
Definition geometry.c:19
void free(void *)
node NULL
Definition grammar.y:163
void * getfree(Freelist *fl)
Definition memory.c:60
void makefree(void *curr, Freelist *fl)
Definition memory.c:83
void freeinit(Freelist *fl, int size)
Definition memory.c:41
static int gcd(int y, int x)
Definition memory.c:28
struct freeblock Freeblock
#define LCM(x, y)
Definition memory.c:39
struct freenode Freenode
struct freenode * nodes
Definition memory.c:21
struct freeblock * next
Definition memory.c:20
Definition mem.h:21
int nodesize
Definition mem.h:24
struct freenode * head
Definition mem.h:22
struct freeblock * blocklist
Definition mem.h:23
struct freenode * nextfree
Definition memory.c:16