Return a non-zero exit code if any stress test fails.
[mpdm.git] / mpdm_v.c
blobce6d0bb735ef3014526362f2bc944994c6a1f7ad
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2011 Angel Ortega <angel@triptico.com>
6 mpdm_v.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"
36 /** data **/
38 /* control structure */
40 struct mpdm_control *mpdm = NULL;
43 /** code **/
45 static void cleanup_value(mpdm_t v)
46 /* cleans a value */
48 /* collapse multiple values */
49 if (v->flags & MPDM_MULTIPLE) {
50 int n;
52 for (n = 0; n < mpdm_size(v); n++)
53 mpdm_unref(mpdm_aget(v, n));
56 /* free data if needed */
57 if (v->data != NULL && v->flags & MPDM_FREE) {
58 free((void *) v->data);
59 v->data = NULL;
62 mpdm->count--;
64 free(v);
68 static void destroy_value(mpdm_t v)
69 /* destroys a value */
71 if (mpdm->threaded_delete) {
72 /* atomically enqueue this value */
73 mpdm_mutex_lock(mpdm->del_queue_mutex);
75 v->next = mpdm->del;
76 mpdm->del = v;
78 mpdm_mutex_unlock(mpdm->del_queue_mutex);
80 /* notify the del queue thread */
81 mpdm_semaphore_post(mpdm->del_queue_sem);
83 else
84 cleanup_value(v);
88 static mpdm_t del_queue_thread(mpdm_t args, mpdm_t ctxt)
89 /* delete queue processing thread */
91 for (;;) {
92 mpdm_t v;
94 /* wait for next value */
95 mpdm_semaphore_wait(mpdm->del_queue_sem);
97 /* do nothing if the queue is empty (should't happen) */
98 if (mpdm->del == NULL)
99 continue;
101 /* atomically dequeue */
102 mpdm_mutex_lock(mpdm->del_queue_mutex);
104 v = mpdm->del;
105 mpdm->del = v->next;
107 mpdm_mutex_unlock(mpdm->del_queue_mutex);
109 cleanup_value(v);
115 * mpdm_new - Creates a new value.
116 * @flags: flags
117 * @data: pointer to real data
118 * @size: size of data
120 * Creates a new value. @flags is an or-ed set of flags, @data is a
121 * pointer to the data the value will store and @size the size of these
122 * data (if value is to be a multiple one, @size is a number of elements,
123 * or a number of bytes otherwise).
125 * This function is normally not directly used; use any of the type
126 * creation macros instead.
127 * [Value Creation]
129 mpdm_t mpdm_new(int flags, const void *data, int size)
131 mpdm_t v = NULL;
133 /* alloc */
134 if ((v = malloc(sizeof(struct mpdm_val))) != NULL) {
135 /* account one value more */
136 mpdm->count++;
138 v->flags = flags;
139 v->ref = 0;
140 v->data = data;
141 v->size = size;
142 v->next = NULL;
145 return v;
150 * mpdm_ref - Increments the reference count of a value.
151 * @v: the value
153 * Increments the reference count of a value.
154 * [Value Management]
156 mpdm_t mpdm_ref(mpdm_t v)
158 if (v != NULL)
159 v->ref++;
161 return v;
166 * mpdm_unref - Decrements the reference count of a value.
167 * @v: the value
169 * Decrements the reference count of a value. If the reference
170 * count of the value reaches 0, it's destroyed.
171 * [Value Management]
173 mpdm_t mpdm_unref(mpdm_t v)
175 if (v != NULL) {
176 v->ref--;
178 if (v->ref <= 0) {
179 destroy_value(v);
180 v = NULL;
184 return v;
189 * mpdm_unrefnd - Decrements the reference count of a value, without destroy.
190 * @v: the value
192 * Decrements the reference count of a value, without destroying
193 * the value if it's unreferenced.
194 * [Value Management]
196 mpdm_t mpdm_unrefnd(mpdm_t v)
198 if (v != NULL)
199 v->ref--;
201 return v;
206 * mpdm_size - Returns the size of an element.
207 * @v: the element
209 * Returns the size of an element. It does not change the
210 * reference count of the value.
211 * [Value Management]
213 int mpdm_size(const mpdm_t v)
215 int r = 0;
217 /* NULL values have no size */
218 if (v != NULL)
219 r = v->size;
221 return r;
226 * mpdm_clone - Creates a clone of a value.
227 * @v: the value
229 * Creates a clone of a value. If the value is multiple, a new value will
230 * be created containing clones of all its elements; otherwise,
231 * the same unchanged value is returned.
232 * [Value Management]
234 mpdm_t mpdm_clone(const mpdm_t v)
236 mpdm_t r;
238 mpdm_ref(v);
240 if (MPDM_IS_ARRAY(v))
241 r = mpdm_aclone(v);
242 else
243 r = v;
245 mpdm_unref(v);
247 return r;
252 * mpdm_root - Returns the root hash.
254 * Returns the root hash. This hash is stored internally and can be used
255 * as a kind of global symbol table.
256 * [Value Management]
258 mpdm_t mpdm_root(void)
260 if (mpdm->root == NULL)
261 mpdm->root = mpdm_ref(MPDM_H(0));
263 return mpdm->root;
268 * mpdm_set_ival - Sets the integer value.
269 * @v: the value
270 * @ival: the integer
272 * Sets the integer value for @v. It does not change
273 * the reference count of @v.
275 mpdm_t mpdm_set_ival(mpdm_t v, int ival)
276 /* sets an integer value to a value */
278 v->flags |= MPDM_IVAL;
279 v->ival = ival;
281 return v;
286 * mpdm_set_rval - Sets the real value.
287 * @v: the value
288 * @rval: the real
290 * Sets the real value for @v. It does not change
291 * the reference count of @v.
293 mpdm_t mpdm_set_rval(mpdm_t v, double rval)
294 /* sets a real value to a value */
296 v->flags |= MPDM_RVAL;
297 v->rval = rval;
299 return v;
304 * mpdm_void - Refs then unrefs a value.
305 * @v: the value
307 * References and unreferences a value. To be used to receive
308 * the output of mpdm_exec() in case of it being void (i.e.
309 * its return value ignored).
311 void mpdm_void(mpdm_t v)
313 mpdm_ref(v);
314 mpdm_unref(v);
319 * mpdm_is_null - Returns 1 if a value is NULL.
320 * @v: the value
322 * Returns 1 if a value is NULL. The reference count is touched.
324 int mpdm_is_null(mpdm_t v)
326 int r;
328 mpdm_ref(v);
329 r = v == NULL ? 1 : 0;
330 mpdm_unref(v);
332 return r;
337 * mpdm_exec - Executes an executable value.
338 * @c: the code value
339 * @args: the arguments
340 * @ctxt: the context
342 * Executes an executable value. If @c is a scalar value, its data
343 * should be a pointer to a directly executable C function with a
344 * prototype of mpdm_t func(mpdm_t args, mpdm_t ctxt); if it's a multiple
345 * one, the first value's data should be a pointer to a directly executable
346 * C function with a prototype of
347 * mpdm_t func(mpdm_t b, mpdm_t args, mpdm_t ctxt) and
348 * the second value will be passed as the @b argument. This value is used
349 * to store bytecode or so when implementing virtual machines or compilers.
350 * The @ctxt is meant to be used as a special context to implement local
351 * symbol tables and such. Its meaning is free and can be NULL.
353 * Returns the return value of the code. If @c is NULL or not executable,
354 * returns NULL.
355 * [Value Management]
357 mpdm_t mpdm_exec(mpdm_t c, mpdm_t args, mpdm_t ctxt)
359 mpdm_t r = NULL;
361 mpdm_ref(c);
362 mpdm_ref(args);
363 mpdm_ref(ctxt);
365 if (c != NULL && (c->flags & MPDM_EXEC)) {
367 if (c->flags & MPDM_MULTIPLE) {
368 mpdm_t x;
369 mpdm_t(*func) (mpdm_t, mpdm_t, mpdm_t);
371 /* value is multiple; first element is the
372 3 argument version of the executable function,
373 next its optional additional information,
374 the arguments and the context */
375 x = mpdm_aget(c, 0);
377 if ((func =
378 (mpdm_t(*)(mpdm_t, mpdm_t, mpdm_t)) (x->data)) != NULL)
379 r = func(mpdm_aget(c, 1), args, ctxt);
381 else {
382 mpdm_t(*func) (mpdm_t, mpdm_t);
384 /* value is scalar; c is the 2 argument
385 version of the executable function */
386 if ((func = (mpdm_t(*)(mpdm_t, mpdm_t)) (c->data)) != NULL)
387 r = func(args, ctxt);
391 mpdm_unref(ctxt);
392 mpdm_unref(args);
393 mpdm_unref(c);
395 return r;
399 mpdm_t mpdm_exec_1(mpdm_t c, mpdm_t a1, mpdm_t ctxt)
401 mpdm_t r;
402 mpdm_t a = MPDM_A(1);
404 mpdm_ref(a);
405 mpdm_aset(a, a1, 0);
407 r = mpdm_exec(c, a, ctxt);
409 mpdm_unref(a);
411 return r;
415 mpdm_t mpdm_exec_2(mpdm_t c, mpdm_t a1, mpdm_t a2, mpdm_t ctxt)
417 mpdm_t r;
418 mpdm_t a = MPDM_A(2);
420 mpdm_ref(a);
421 mpdm_aset(a, a1, 0);
422 mpdm_aset(a, a2, 1);
424 r = mpdm_exec(c, a, ctxt);
426 mpdm_unref(a);
428 return r;
432 mpdm_t mpdm_exec_3(mpdm_t c, mpdm_t a1, mpdm_t a2, mpdm_t a3, mpdm_t ctxt)
434 mpdm_t r;
435 mpdm_t a = MPDM_A(3);
437 mpdm_ref(a);
438 mpdm_aset(a, a1, 0);
439 mpdm_aset(a, a2, 1);
440 mpdm_aset(a, a3, 2);
442 r = mpdm_exec(c, a, ctxt);
444 mpdm_unref(a);
446 return r;
450 mpdm_t mpdm_xnew(mpdm_t(*a1) (mpdm_t, mpdm_t, mpdm_t), mpdm_t a2)
452 mpdm_t x;
454 x = MPDM_A(2);
455 x->flags |= MPDM_EXEC;
457 mpdm_ref(x);
459 mpdm_aset(x, MPDM_X(a1), 0);
460 mpdm_aset(x, a2, 1);
462 mpdm_unrefnd(x);
464 return x;
468 mpdm_t mpdm_new_copy(int flags, void *ptr, int size)
470 mpdm_t r = NULL;
472 if (ptr != NULL) {
473 char *ptr2 = malloc(size);
474 memcpy(ptr2, ptr, size);
476 r = mpdm_new(MPDM_FREE | flags, ptr2, size);
479 return r;
483 static mpdm_t MPDM(const mpdm_t args, mpdm_t ctxt)
484 /* accesor / mutator for MPDM internal data */
486 mpdm_t v;
488 mpdm_ref(args);
490 v = mpdm_aget(args, 0);
492 if (v != NULL) {
493 mpdm_t w;
495 /* do changes */
496 if ((w = mpdm_hget_s(v, L"hash_buckets")) != NULL)
497 mpdm->hash_buckets = mpdm_ival(w);
498 else
499 if ((w = mpdm_hget_s(v, L"threaded_delete")) != NULL)
500 mpdm->threaded_delete = mpdm_ival(w);
503 /* now collect all information */
504 v = MPDM_H(0);
506 mpdm_ref(v);
508 mpdm_hset_s(v, L"version", MPDM_MBS(VERSION));
509 mpdm_hset_s(v, L"count", MPDM_I(mpdm->count));
510 mpdm_hset_s(v, L"hash_buckets", MPDM_I(mpdm->hash_buckets));
511 mpdm_hset_s(v, L"threaded_delete", MPDM_I(mpdm->threaded_delete));
513 mpdm_unref(args);
515 mpdm_unrefnd(v);
517 return v;
520 extern char **environ;
522 static mpdm_t build_env(void)
523 /* builds a hash with the environment */
525 char **ptr;
526 mpdm_t e = MPDM_H(0);
528 mpdm_ref(e);
530 for (ptr = environ; *ptr != NULL; ptr++) {
531 char *eq = strchr(*ptr, '=');
533 if (eq != NULL) {
534 mpdm_t k, v;
536 k = MPDM_NMBS((*ptr), eq - (*ptr));
537 v = MPDM_MBS(eq + 1);
539 mpdm_hset(e, k, v);
543 mpdm_unrefnd(e);
545 return e;
550 * mpdm_startup - Initializes MPDM.
552 * Initializes the Minimum Profit Data Manager. Returns 0 if
553 * everything went OK.
555 int mpdm_startup(void)
557 /* do the startup only unless done beforehand */
558 if (mpdm == NULL) {
559 /* alloc space */
560 if ((mpdm = malloc(sizeof(struct mpdm_control))) == NULL)
561 return -1;
563 /* cleans it */
564 memset(mpdm, '\0', sizeof(struct mpdm_control));
566 /* sets the defaults */
567 mpdm->hash_buckets = 31;
569 /* sets the threaded delete control */
570 mpdm->threaded_delete = 0;
572 /* create the threaded delete thread and control */
573 mpdm->del_queue_mutex = mpdm_new_mutex();
574 mpdm->del_queue_sem = mpdm_new_semaphore(0);
575 mpdm_exec_thread(MPDM_X(del_queue_thread), NULL, NULL);
577 /* sets the locale */
578 if (setlocale(LC_ALL, "") == NULL)
579 setlocale(LC_ALL, "C");
581 mpdm_encoding(NULL);
583 /* store the MPDM() function */
584 mpdm_hset_s(mpdm_root(), L"MPDM", MPDM_X(MPDM));
586 /* store the ENV hash */
587 mpdm_hset_s(mpdm_root(), L"ENV", build_env());
590 /* everything went OK */
591 return 0;
596 * mpdm_shutdown - Shuts down MPDM.
598 * Shuts down MPDM. No MPDM functions should be used from now on.
600 void mpdm_shutdown(void)
602 /* dummy, by now */
606 * MPDM_A - Creates an array value.
607 * @n: Number of elements
609 * Creates a new array value with @n elements.
610 * [Value Creation]
612 /** mpdm_t MPDM_A(int n); */
613 /* ; */
616 * MPDM_H - Creates a hash value.
617 * @n: Number of buckets in the hash (0: use default)
619 * Creates a new hash value with @n buckets. The number
620 * of buckets must be a prime number. If @n is 0, an
621 * optimal number of buckets will be used.
622 * [Value Creation]
624 /** mpdm_t MPDM_H(int n); */
625 /* ; */
628 * MPDM_LS - Creates a string value from a literal string.
629 * @wcs: the wide character string
631 * Creates a new string value from a literal, wide character string.
632 * A pointer to the string will be stored in the value (not a copy).
633 * [Value Creation]
635 /** mpdm_t MPDM_LS(wchar_t * wcs); */
636 /* ; */
639 * MPDM_S - Creates a string value from a string.
640 * @wcs: the wide character string
642 * Creates a new string value from a wide character string. The value
643 * will store a copy of the string that will be freed on destruction.
644 * [Value Creation]
646 /** mpdm_t MPDM_S(wchar_t * wcs); */
647 /* ; */
650 * MPDM_NS - Creates a string value from a string, with size.
651 * @wcs: the wide character string
652 * @s: the size in chars the string will hold
654 * Creates a new string value with a copy of the first @s characters
655 * from the @wcs string.
656 * [Value Creation]
658 /** mpdm_t MPDM_NS(wchar_t * wcs, int s); */
659 /* ; */
662 * MPDM_ENS - Creates a string value from an external string, with size.
663 * @wcs: the external wide character string
664 * @s: the size in chars the string will hold
666 * Creates a new string value with size @s. The @wcs string must be
667 * a dynamic value (i.e. allocated by malloc()) that will be freed on
668 * destruction.
669 * [Value Creation]
671 /** mpdm_t MPDM_ENS(wchar_t * wcs, int s); */
672 /* ; */
675 * MPDM_I - Creates an integer value.
676 * @i: the integer
678 * Creates a new integer value. MPDM integers are strings.
679 * [Value Creation]
681 /** mpdm_t MPDM_I(int i); */
682 /* ; */
685 * MPDM_R - Creates a real value.
686 * @r: the real number
688 * Creates a new real value. MPDM integers are strings.
689 * [Value Creation]
691 /** mpdm_t MPDM_R(double r); */
692 /* ; */
695 * MPDM_F - Creates a file value.
696 * @f: the file descriptor
698 * Creates a new file value.
699 * [Value Creation]
701 /** mpdm_t MPDM_F(FILE * f); */
702 /* ; */
705 * MPDM_MBS - Creates a string value from a multibyte string.
706 * @mbs: the multibyte string
708 * Creates a new string value from a multibyte string, that will be
709 * converted to wcs by mpdm_mbstowcs().
710 * [Value Creation]
712 /** mpdm_t MPDM_MBS(char * mbs); */
713 /* ; */
716 * MPDM_NMBS - Creates a string value from a multibyte string, with size.
717 * @mbs: the multibyte string
718 * @s: the size
720 * Creates a new string value with the first @s characters from the @mbs
721 * multibyte string, that will be converted to wcs by mpdm_mbstowcs().
722 * [Value Creation]
724 /** mpdm_t MPDM_NMBS(char * mbs, int s); */
725 /* ; */
728 * MPDM_2MBS - Creates a multibyte string value from a wide char string.
729 * @wcs: the wide char string
731 * Creates a multibyte string value from the @wcs wide char string,
732 * converting it by mpdm_wcstombs(). Take note that multibyte string values
733 * are not properly strings, so they cannot be used for string comparison
734 * and such.
735 * [Value Creation]
737 /** mpdm_t MPDM_2MBS(wchar_t * wcs); */
738 /* ; */
741 * MPDM_X - Creates a new executable value.
742 * @func: the C code function
744 * Creates a new executable value given a pointer to the @func C code function.
745 * The function must receive an mpdm_t array value (that will hold their
746 * arguments) and return another one.
747 * [Value Creation]
749 /** mpdm_t MPDM_X(mpdm_t (* func)(mpdm_t args)); */
750 /* ; */
753 * MPDM_C - Creates a new value with a copy of a buffer.
754 * @flags: additional flags
755 * @ptr: pointer to data
756 * @size: data size
758 * Create a new value with a copy of a buffer. The value will store a copy
759 * of @ptr and have the additional @flags.
760 * [Value Creation]
762 /** mpdm_t MPDM_C(int flags, void *ptr, int size); */
763 /* ; */
766 * MPDM_IS_ARRAY - Tests if a value is an array.
767 * @v: the value
769 * Returns non-zero if @v is an array.
771 /** int MPDM_IS_ARRAY(mpdm_t v); */
772 /* ; */
775 * MPDM_IS_HASH - Tests if a value is a hash.
776 * @v: the value
778 * Returns non-zero if @v is a hash.
780 /** int MPDM_IS_HASH(mpdm_t v); */
781 /* ; */
784 * MPDM_IS_EXEC - Tests if a value is executable.
785 * @v: the value
787 * Returns non-zero if @v is executable.
789 /** int MPDM_IS_EXEC(mpdm_t v); */
790 /* ; */
793 * MPDM_IS_STRING - Tests if a value is a string.
794 * @v: the value
796 * Returns non-zero if @v is a string.
798 /** int MPDM_IS_STRING(mpdm_t v); */
799 /* ; */