The environment is stored in a hash called ENV in mpdm_root().
[mpdm.git] / mpdm_v.c
blob80039bfbe8903a346fd1249564f37004eca97839
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2007 Angel Ortega <angel@triptico.com>
6 mpdm_t.c - Basic value management
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <locale.h>
33 #include "mpdm.h"
35 /*******************
36 Data
37 ********************/
39 /* control structure */
41 struct mpdm_control *mpdm = NULL;
43 /*******************
44 Code
45 ********************/
47 static void cleanup_value(mpdm_t v)
48 /* cleans a value */
50 /* collapse multiple values */
51 if (v->flags & MPDM_MULTIPLE)
52 mpdm_collapse(v, 0, v->size);
54 /* free data if needed */
55 if (v->data != NULL && v->flags & MPDM_FREE) {
56 mpdm->memory_usage -= v->size;
57 free((void *)v->data);
62 int mpdm_destroy(mpdm_t v)
63 /* destroys a value */
65 /* if still referenced, don't do it */
66 if (v->ref)
67 return 0;
69 /* dequeue */
70 v->next->prev = v->prev;
71 v->prev->next = v->next;
73 /* if it's the current one, move to next */
74 if (mpdm->cur == v)
75 mpdm->cur = v->next;
77 /* account one value less */
78 mpdm->count--;
80 /* add to the deleted values queue */
81 v->next = mpdm->del;
82 mpdm->del = v;
84 cleanup_value(v);
86 return 1;
90 /**
91 * mpdm_new - Creates a new value.
92 * @flags: flags
93 * @data: pointer to real data
94 * @size: size of data
96 * Creates a new value. @flags is an or-ed set of flags, @data is a
97 * pointer to the data the value will store and @size the size of these
98 * data (if value is to be a multiple one, @size is a number of elements,
99 * or a number of bytes otherwise).
101 * This function is normally not directly used; use any of the type
102 * creation macros instead.
103 * [Value Creation]
105 mpdm_t mpdm_new(int flags, const void *data, int size)
107 mpdm_t v = NULL;
109 /* alloc */
110 if ((v = mpdm->del) != NULL)
111 mpdm->del = v->next;
112 else
113 if ((v = malloc(sizeof(struct mpdm_val))) == NULL)
114 return NULL;
116 /* add to the circular list */
117 if (mpdm->cur == NULL)
118 v->next = v->prev = v;
119 else {
120 v->prev = mpdm->cur;
121 v->next = mpdm->cur->next;
123 v->prev->next = v->next->prev = v;
126 mpdm->cur = v->next;
128 /* account one value more */
129 mpdm->count++;
131 /* count memory if data is dynamic */
132 if (flags & MPDM_FREE)
133 mpdm->memory_usage += size;
135 v->flags = flags;
136 v->ref = 0;
137 v->data = data;
138 v->size = size;
140 return v;
145 * mpdm_ref - Increments the reference count of a value.
146 * @v: the value
148 * Increments the reference count of a value.
149 * [Value Management]
151 mpdm_t mpdm_ref(mpdm_t v)
153 if (v != NULL)
154 v->ref++;
155 return v;
160 * mpdm_unref - Decrements the reference count of a value.
161 * @v: the value
163 * Decrements the reference count of a value.
164 * [Value Management]
166 mpdm_t mpdm_unref(mpdm_t v)
168 if (v != NULL)
169 v->ref--;
170 return v;
175 * mpdm_sweep - Sweeps unreferenced values.
176 * @count: number of values to be swept
178 * Destroys values with a reference count of 0. @count is the
179 * number of values to be checked for deletion; special values of
180 * @count are -1, that forces a check of all currently known values
181 * (can be time-consuming) and 0, which tells mpdm_sweep() to check a
182 * small group of them on each call.
183 * [Value Management]
185 void mpdm_sweep(int count)
187 /* if count is zero, sweep 'some' values */
188 if (count == 0) {
189 if (mpdm->default_sweep < 0)
190 count = mpdm->count / -mpdm->default_sweep;
191 else
192 count = mpdm->default_sweep;
195 /* if count is -1, sweep all */
196 if (count == -1)
197 count = mpdm->count;
199 for (; count > 0 && mpdm->count > mpdm->low_threshold; count--) {
200 /* destroy it or skip it */
201 if (!mpdm_destroy(mpdm->cur))
202 mpdm->cur = mpdm->cur->next;
208 * mpdm_size - Returns the size of an element.
209 * @v: the element
211 * Returns the size of an element.
212 * [Value Management]
214 int mpdm_size(const mpdm_t v)
216 /* NULL values have no size */
217 if (v == NULL)
218 return 0;
220 return v->size;
225 * mpdm_clone - Creates a clone of a value.
226 * @v: the value
228 * Creates a clone of a value. If the value is multiple, a new value will
229 * be created containing clones of all its elements; otherwise,
230 * the same unchanged value is returned.
231 * [Value Management]
233 mpdm_t mpdm_clone(const mpdm_t v)
235 if (MPDM_IS_ARRAY(v))
236 return mpdm_aclone(v);
238 return v;
243 * mpdm_root - Returns the root hash.
245 * Returns the root hash. This hash is stored internally and can be used
246 * as a kind of global symbol table.
247 * [Value Management]
249 mpdm_t mpdm_root(void)
251 if (mpdm->root == NULL)
252 mpdm->root = mpdm_ref(MPDM_H(0));
254 return mpdm->root;
258 mpdm_t mpdm_set_ival(mpdm_t v, int ival)
259 /* sets an integer value to a value */
261 v->flags |= MPDM_IVAL;
262 v->ival = ival;
264 return v;
268 mpdm_t mpdm_set_rval(mpdm_t v, double rval)
269 /* sets a real value to a value */
271 v->flags |= MPDM_RVAL;
272 v->rval = rval;
274 return v;
279 * mpdm_exec - Executes an executable value.
280 * @c: the code value
281 * @args: the arguments
283 * Executes an executable value. If @c is a scalar value, its data
284 * should be a pointer to a directly executable C function with a
285 * prototype of mpdm_t func(mpdm_t args); if it's a multiple one,
286 * the first value's data should be a pointer to a directly executable C
287 * function with a prototype of mpdm_t func(mpdm_t b, mpdm_t args) and
288 * the second value will be passed as the @b argument. This value is used
289 * to store bytecode or so when implementing virtual machines or compilers.
291 * Returns the return value of the code. If @c is NULL or not executable,
292 * returns NULL.
293 * [Value Management]
295 mpdm_t mpdm_exec(mpdm_t c, mpdm_t args)
297 mpdm_t r = NULL;
299 if (c != NULL && (c->flags & MPDM_EXEC)) {
300 mpdm_ref(c);
301 mpdm_ref(args);
303 if (c->flags & MPDM_MULTIPLE) {
304 mpdm_t x;
305 mpdm_t(*func) (mpdm_t, mpdm_t);
307 /* value is multiple; first element is the
308 2 argument version of the executable function,
309 next its optional additional information and
310 finally the arguments */
311 x = mpdm_aget(c, 0);
313 if ((func = (mpdm_t(*)(mpdm_t, mpdm_t)) (x->data)) != NULL)
314 r = func(mpdm_aget(c, 1), args);
316 else {
317 mpdm_t(*func) (mpdm_t);
319 /* value is scalar; c is the 1 argument
320 version of the executable function */
321 if ((func = (mpdm_t(*)(mpdm_t)) (c->data)) != NULL)
322 r = func(args);
325 mpdm_unref(args);
326 mpdm_unref(c);
329 return r;
333 mpdm_t mpdm_exec_1(mpdm_t c, mpdm_t a1)
335 mpdm_t r;
336 mpdm_t a = MPDM_A(1);
338 mpdm_aset(a, a1, 0);
339 r = mpdm_exec(c, a);
340 mpdm_destroy(a);
341 return r;
345 mpdm_t mpdm_exec_2(mpdm_t c, mpdm_t a1, mpdm_t a2)
347 mpdm_t r;
348 mpdm_t a = MPDM_A(2);
350 mpdm_aset(a, a1, 0);
351 mpdm_aset(a, a2, 1);
352 r = mpdm_exec(c, a);
353 mpdm_destroy(a);
354 return r;
358 mpdm_t mpdm_exec_3(mpdm_t c, mpdm_t a1, mpdm_t a2, mpdm_t a3)
360 mpdm_t r;
361 mpdm_t a = MPDM_A(3);
363 mpdm_aset(a, a1, 0);
364 mpdm_aset(a, a2, 1);
365 mpdm_aset(a, a3, 2);
366 r = mpdm_exec(c, a);
367 mpdm_destroy(a);
368 return r;
372 mpdm_t mpdm_xnew(mpdm_t(*a1) (mpdm_t, mpdm_t), mpdm_t a2)
374 mpdm_t x;
376 x = MPDM_A(2);
377 x->flags |= MPDM_EXEC;
379 mpdm_aset(x, MPDM_X(a1), 0);
380 mpdm_aset(x, a2, 1);
382 return x;
386 static mpdm_t MPDM(const mpdm_t args)
387 /* accesor / mutator for MPDM internal data */
389 mpdm_t v = mpdm_aget(args, 0);
390 int n, c = 0;
392 if (v != NULL) {
393 mpdm_t w;
395 /* do changes */
396 if ((w = mpdm_hget_s(v, L"low_threshold")) != NULL && mpdm_ival(w) > 0)
397 mpdm->low_threshold = mpdm_ival(w);
399 if ((w = mpdm_hget_s(v, L"default_sweep")) != NULL)
400 mpdm->default_sweep = mpdm_ival(w);
402 if ((w = mpdm_hget_s(v, L"hash_buckets")) != NULL)
403 mpdm->hash_buckets = mpdm_ival(w);
406 /* loop all values counting the references ones */
407 for (n = mpdm->count, v = mpdm->cur; n > 0; n--, v = v->next)
408 if (v->ref == 0)
409 c++;
411 /* now collect all information */
412 v = MPDM_H(0);
414 mpdm_hset_s(v, L"version", MPDM_MBS(VERSION));
415 mpdm_hset_s(v, L"count", MPDM_I(mpdm->count));
416 mpdm_hset_s(v, L"low_threshold", MPDM_I(mpdm->low_threshold));
417 mpdm_hset_s(v, L"default_sweep", MPDM_I(mpdm->default_sweep));
418 mpdm_hset_s(v, L"memory_usage", MPDM_I(mpdm->memory_usage));
419 mpdm_hset_s(v, L"hash_buckets", MPDM_I(mpdm->hash_buckets));
420 mpdm_hset_s(v, L"unreferenced", MPDM_I(c));
422 return v;
425 extern char **environ;
427 static mpdm_t build_env(void)
428 /* builds a hash with the environment */
430 char **ptr;
431 mpdm_t e = MPDM_H(0);
433 for (ptr = environ; *ptr != NULL; ptr++) {
434 char *eq = strchr(*ptr, '=');
436 if (eq != NULL) {
437 mpdm_t k, v;
439 k = MPDM_NMBS((*ptr), eq - (*ptr));
440 v = MPDM_MBS(eq + 1);
442 mpdm_hset(e, k, v);
446 return e;
451 * mpdm_startup - Initializes MPDM.
453 * Initializes the Minimum Profit Data Manager. Returns 0 if
454 * everything went OK.
456 int mpdm_startup(void)
458 /* do the startup only unless done beforehand */
459 if (mpdm == NULL) {
460 /* alloc space */
461 if ((mpdm = malloc(sizeof(struct mpdm_control))) == NULL)
462 return -1;
464 /* cleans it */
465 memset(mpdm, '\0', sizeof(struct mpdm_control));
467 /* sets the defaults */
468 mpdm->low_threshold = 16;
469 mpdm->default_sweep = -50000;
470 mpdm->hash_buckets = 31;
472 /* sets the locale */
473 if (setlocale(LC_ALL, "") == NULL)
474 setlocale(LC_ALL, "C");
476 mpdm_encoding(NULL);
478 /* store the MPDM() function */
479 mpdm_hset_s(mpdm_root(), L"MPDM", MPDM_X(MPDM));
481 /* store the ENV hash */
482 mpdm_hset_s(mpdm_root(), L"ENV", build_env());
485 /* everything went OK */
486 return 0;
491 * mpdm_shutdown - Shuts down MPDM.
493 * Shuts down MPDM. No MPDM functions should be used from now on.
495 void mpdm_shutdown(void)
497 /* dummy, by now */
501 * MPDM_A - Creates an array value.
502 * @n: Number of elements
504 * Creates a new array value with @n elements.
505 * [Value Creation]
507 /** mpdm_t MPDM_A(int n); */
508 /* ; */
511 * MPDM_H - Creates a hash value.
512 * @n: Number of buckets in the hash (0: use default)
514 * Creates a new hash value with @n buckets. The number
515 * of buckets must be a prime number. If @n is 0, an
516 * optimal number of buckets will be used.
517 * [Value Creation]
519 /** mpdm_t MPDM_H(int n); */
520 /* ; */
523 * MPDM_LS - Creates a string value from a literal string.
524 * @wcs: the wide character string
526 * Creates a new string value from a literal, wide character string.
527 * A pointer to the string will be stored in the value (not a copy).
528 * [Value Creation]
530 /** mpdm_t MPDM_LS(wchar_t * wcs); */
531 /* ; */
534 * MPDM_S - Creates a string value from a string.
535 * @wcs: the wide character string
537 * Creates a new string value from a wide character string. The value
538 * will store a copy of the string that will be freed on destruction.
539 * [Value Creation]
541 /** mpdm_t MPDM_S(wchar_t * wcs); */
542 /* ; */
545 * MPDM_NS - Creates a string value from a string, with size.
546 * @wcs: the wide character string
547 * @s: the size in chars the string will hold
549 * Creates a new string value with a copy of the first @s characters
550 * from the @wcs string.
551 * [Value Creation]
553 /** mpdm_t MPDM_NS(wchar_t * wcs, int s); */
554 /* ; */
557 * MPDM_ENS - Creates a string value from an external string, with size.
558 * @wcs: the external wide character string
559 * @s: the size in chars the string will hold
561 * Creates a new string value with size @s. The @wcs string must be
562 * a dynamic value (i.e. allocated by malloc()) that will be freed on
563 * destruction.
564 * [Value Creation]
566 /** mpdm_t MPDM_ENS(wchar_t * wcs, int s); */
567 /* ; */
570 * MPDM_I - Creates an integer value.
571 * @i: the integer
573 * Creates a new integer value. MPDM integers are strings.
574 * [Value Creation]
576 /** mpdm_t MPDM_I(int i); */
577 /* ; */
580 * MPDM_R - Creates a real value.
581 * @r: the real number
583 * Creates a new real value. MPDM integers are strings.
584 * [Value Creation]
586 /** mpdm_t MPDM_R(double r); */
587 /* ; */
590 * MPDM_F - Creates a file value.
591 * @f: the file descriptor
593 * Creates a new file value.
594 * [Value Creation]
596 /** mpdm_t MPDM_F(FILE * f); */
597 /* ; */
600 * MPDM_MBS - Creates a string value from a multibyte string.
601 * @mbs: the multibyte string
603 * Creates a new string value from a multibyte string, that will be
604 * converted to wcs by mpdm_mbstowcs().
605 * [Value Creation]
607 /** mpdm_t MPDM_MBS(char * mbs); */
608 /* ; */
611 * MPDM_NMBS - Creates a string value from a multibyte string, with size.
612 * @mbs: the multibyte string
613 * @s: the size
615 * Creates a new string value with the first @s characters from the @mbs
616 * multibyte string, that will be converted to wcs by mpdm_mbstowcs().
617 * [Value Creation]
619 /** mpdm_t MPDM_NMBS(char * mbs, int s); */
620 /* ; */
623 * MPDM_2MBS - Creates a multibyte string value from a wide char string.
624 * @wcs: the wide char string
626 * Creates a multibyte string value from the @wcs wide char string,
627 * converting it by mpdm_wcstombs(). Take note that multibyte string values
628 * are not properly strings, so they cannot be used for string comparison
629 * and such.
630 * [Value Creation]
632 /** mpdm_t MPDM_2MBS(wchar_t * wcs); */
633 /* ; */
636 * MPDM_X - Creates a new executable value.
637 * @func: the C code function
639 * Creates a new executable value given a pointer to the @func C code function.
640 * The function must receive an mpdm_t array value (that will hold their
641 * arguments) and return another one.
642 * [Value Creation]
644 /** mpdm_t MPDM_X(mpdm_t (* func)(mpdm_t args)); */
645 /* ; */
648 * MPDM_IS_ARRAY - Tests if a value is an array.
649 * @v: the value
651 * Returns non-zero if @v is an array.
653 /** int MPDM_IS_ARRAY(mpdm_t v); */
654 /* ; */
657 * MPDM_IS_HASH - Tests if a value is a hash.
658 * @v: the value
660 * Returns non-zero if @v is a hash.
662 /** int MPDM_IS_HASH(mpdm_t v); */
663 /* ; */
666 * MPDM_IS_EXEC - Tests if a value is executable.
667 * @v: the value
669 * Returns non-zero if @v is executable.
671 /** int MPDM_IS_EXEC(mpdm_t v); */
672 /* ; */
675 * MPDM_IS_STRING - Tests if a value is a string.
676 * @v: the value
678 * Returns non-zero if @v is a string.
680 /** int MPDM_IS_STRING(mpdm_t v); */
681 /* ; */