File: mvector.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 (90 lines) | stat: -rw-r--r-- 2,191 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jvmpi.h>

#include <jmp.h>
#include <mvector.h>
#include <method.h>

/** Create a new mvector with can hold at least size elements. */
mvector* mvector_new (size_t size) {
    mvector* mv = malloc (sizeof (*mv));
    if (mv == NULL)
	return NULL;
    mv->methods = malloc (sizeof (method*) * size);
    if (mv->methods == NULL) {
	mvector_free (mv);
	return NULL;
    }
    mv->size = size;
    mv->load = 0;
    return mv;
}

/** Delete the given mvector. */
void mvector_free (mvector* mv) {
    if (mv == NULL)
	return;
    free (mv->methods);
    free (mv);
}

/** Get the number of methods in this vector. */
inline size_t mvector_load (mvector* mv) {
    return mv->load;
}

size_t mvector_grow (mvector* mv) {
    method** nm = realloc (mv->methods, mv->size * 2 * sizeof (method*));
    if (nm == NULL)
	return 0;
    mv->methods = nm;
    mv->size *= 2;
    return mv->size;
}

/** Add a new method to this vector. 
 * @return the load of the new vector.
 */
size_t mvector_add_method (mvector* mv, method* m) {
    if (mv->load == mv->size) {
	if (!mvector_grow (mv)) {
	    fprintf (stderr, "failed to grow mvector...\n");
	    return -1;
	}
    }
    mv->methods[mv->load] = m;
    mv->load++;
    return mv->load;
}

/** search for a method in the given vector.
 * @param mv the vector to search in.
 * @param method the method to search for.
 * @return the index of the method or -1 if not found.
 */
long mvector_search (mvector* mv, method* m) {
    long i;
    for (i = 0; i < (long)mv->load; i++) {
	if (mv->methods[i] == m)
	    return i;
    }
    return -1;
}

/** Get the i:th method. */
inline method* mvector_get (mvector* mv, size_t i) {
    if (i >= mv->load)
	return NULL;
    return (mv->methods[i]);
}


/* 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: */