Menu

[7c54b3]: / joe / obj.h  Maximize  Restore  History

Download this file

187 lines (122 with data), 5.1 kB

  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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Global object stack, plus dynamic string and dynamic array functions
* built on top of them.
* Copyright
* (C) 2006 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
/* Object structure */
struct obj {
LINK(Obj) link; /* Memory dlist object is on */
int size; /* Alloc size of object */
int len; /* Length of object */
union {
double d;
char c;
int i;
long l;
void *p;
} mem[1]; /* Data of the object, aligned properly */
};
extern Obj obj_stack[1]; /* The stack */
/* Get hidden header size */
#define obj_hdr_size ((char *)obj_stack->mem - (char *)obj_stack)
/* Get object base address from normal address */
#define obj_base(o) ((Obj *)((char *)(o) - obj_hdr_size))
/* Round up to min size, which should be a power of 2 */
#define round_up(n, to) (((n) + to) & ~(to - 1))
Obj get_obj_stack(); /* Get current stack, setup a new one in its place */
void set_obj_stack(Obj o); /* Restore a saved stack. */
/* Object memory allocation */
/* Free specified object. If object is on the stack, all more recently
* allocated objects are freed as well. It's OK to call sfree(NULL). */
void obj_free(const void *ptr);
/* Allocate an object. If you request zero bytes, a minimum sized block will
be allocated. The object is allocated on the stack. */
void *obj_malloc(int size);
/* Reallocate a block. It's OK to request 0 bytes (a minimum sized block is
* allocated). 'ptr' must be to a block already allocated with salloc().
* Sreallocing a block does not change it's allocation order: sfree cares
* about the order of salloc()s only, and does not care about srealloc().
* */
void *obj_realloc(void *ptr, int new_size);
/* Turn object from stack allocated to heap allocated */
void obj_perm(const void *ptr);
/* Turn object from heap allocated to stack allocated */
void obj_temp(void *ptr);
/* Return true if object is a heap object, false if stack */
int obj_isperm(const void *ptr);
/* Reference length part of object */
#define obj_len(a) (obj_base(a)->len)
/* Reference malloc size part of object */
#define obj_size(a) (obj_base(a)->size)
/** Variable strings **/
/* Create a dynamic string with enough space for a string of indicated length. The created
* string will have a length of 0. */
char *vsmk(int prealloc);
/* Get length of string, NULL allowed for s */
int vslen(const char *s);
/* Make sure there's enough space in the array for 'len' elements. Whenever
* vsensure reallocs the array, it allocates 25% more than the necessary
* minimum space in anticipation of future expansion. If 'vary' is 0,
* it creates a new array. */
char *vsensure(char *s, int len);
/* Truncate array to indicated size. This expands the string with blank
* elements if necessary and sets the vslen() of the array. A new array is
* created if 'vary' is 0. */
char *vstrunc(char *s, int len);
/* Copy array/len to string starting at offset pos. String is expanded if
* necessary to fit the array. If pos is beyond length of string, string is
* expanded with blanks. */
char *vsncpy(char *s, int pos, const char *array, int len);
/* Write a block of len ch characters starting at offset pos. If pos is
beyond length of string, the string is expanded with blanks. */
char *vsfill(char *s, int pos, int ch, int len);
/* Copy an array onto a string. Same as: vstrunc(s,0), vscat(s,array). */
char *vscpy(char *s, char *array, int len);
char *vscpyz(char *s, const char *z);
/* Concatenate an array of characters to a string. */
char *vscat(char *s, char *array, int len);
char *vscatz(char *s, const char *z);
char *vsdup(const char *s);
char *vsdupz(const char *z);
/* Append a single character to a string */
char *_vsadd(char *s, char c);
#define vsadd(s, c) ( (s) && (obj_len(s) + 1) < obj_size(s) ? ((s)[obj_len(s)++] = c, (s)[obj_len(s)] = 0, (s)) : _vsadd((s), (c)) )
/* Assign a single character to a position in a string */
char *_vsset(char *vary, int pos, char el);
#define vsset(v, p, el) \
(!(v) || (p) > obj_len(v) || (p) >= obj_size(v) ? \
_vsset((v), (p), (el)) \
: \
((p) == obj_len(v) ? \
((v)[(p) + 1] = 0, obj_len(v) = (p) + 1, (v)[p] = (el), (v)) \
: \
((v)[p] = (el), (v)) \
) \
)
/* Get line of input */
char *vsgets(char **s, FILE *f);
char **isfree(char **s);
int vsscan (char *a, int alen, char *b, int blen);
int vsspan (char *a, int alen, char *b, int blen);
int vsbsearch (char *ary, int len, char el);
/* Printf to variable string */
char *vsfmt(char *vary, int pos, const char *format, ...);
/** Variable arrays **/
char **vamk(int len);
void varm(char **vary);
char **vaensure(char **vary, int len);
char **vazap(char **vary, int pos, int n);
char **vatrunc(char **vary, int len);
char **vaadd(char **vary, char *s);
void vasort(char **vary, int len);
void vaisort(char **vary, int len);
char **vawords(char **vary, char *s, int len, char *sep, int seplen);
#define sz(s) (s), zlen(s)
#define sv(s) (s), vslen(s)
#define sc(s) (char *)(s), (sizeof(s)-1)
#define av(a) (a), valen(a)
int valen(char **a);
void vaperm(char **a);
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.