File: objectstore.c

package info (click to toggle)
jmp 0.48-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,692 kB
  • ctags: 1,732
  • sloc: ansic: 13,985; sh: 8,611; makefile: 526; yacc: 318; java: 18
file content (108 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jvmpi.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <obj.h>
#include <objectstore.h>

/** Create a new objectstore.
 */
objectstore* objectstore_new () {
    objectstore* os;
    os = malloc (sizeof (*os));
    if (!os) {
	fprintf(stderr,"objectstore_new: out of memory\n");
	exit(1);
    }

    os->free_list = NULL;
    os->buf = NULL;
    os->size = 0;
    return os;
}

/** Delete the given objectstore. */
void objectstore_free (objectstore* os) {    
    obj_buffer* oo;
    if (os == NULL)
	return;
    while ((oo = os->buf)) {
	os->buf = os->buf->next;
	free (oo->buf);
	free (oo);
    }
    free (os);
}

static void create_new_buffer (objectstore* os) {
    obj_buffer* nb;
    int i;
    int size = 8192;
    nb = malloc (sizeof (*nb));
    if (nb == NULL)
	return;
    nb->next = os->buf;
    os->buf = nb;
    nb->buf = malloc (sizeof (*nb->buf) * size);
    if (nb->buf == NULL) {
	free (nb);
	return;
    }
    for (i = 0; i < size - 1; i++) 
	nb->buf[i].next_free = nb->buf + i + 1;
    nb->buf[i].next_free = os->free_list;
    os->free_list = nb->buf;    
}

/** Create a new object on the objectstore. Return the array index.
 */
obj* objectstore_obj_new (objectstore* os, jint arena_id, cls* class_id, 
			  jint is_array, jint size, jobjectID obj_id, 
			  method* method, int reset_level, int gc_level) {
    obj_obs* ret;
    
    if (os->free_list == NULL) {
	//fprintf (stderr, "free_list empty, allocating new...\n");
	create_new_buffer (os);
    }
    ret = os->free_list;
    if (!ret) {
	fprintf(stderr,"objectstore_obj_new: out of memory!\n");
	exit(1);
    }
    os->free_list = ret->next_free;
    os->size++;
    
    ret->o.arena_id = arena_id;
    ret->o.clz = class_id;
    ret->o.is_array = is_array;
    ret->o.size = size;
    ret->o.obj_id = obj_id;
    ret->o.method = method;
    ret->o.reset_level = reset_level;
    ret->o.gc_level = gc_level;
    return (obj*)ret;
}

/** Destroy an object on the objectstore
 */
void objectstore_obj_free (objectstore* os, obj* o) {
    obj_obs* oo = (obj_obs*)o;
    oo->next_free = os->free_list;
    os->free_list = oo;
    os->size--;
}


/* Emacs Local Variables: */
/* Emacs mode:C */
/* Emacs c-indentation-style:"gnu" */
/* Emacs c-hanging-braces-alist:((brace-list-open)(brace-entry-open)(defun-open after)(substatement-open after)(block-close . c-snug-do-while)(extern-lang-open after)) */
/* Emacs c-cleanup-list:(brace-else-brace brace-elseif-brace space-before-funcall) */
/* Emacs c-basic-offset:4 */
/* Emacs End: */