Threaded delete can be toggled in MPDM().
[mpdm.git] / mpdm_v.c
blobd99ba3132d53fe84d15dfb821efb7c8ad99127a1
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 mpdm_collapse(v, 0, v->size);
52 /* free data if needed */
53 if (v->data != NULL && v->flags & MPDM_FREE) {
54 free((void *) v->data);
55 v->data = NULL;
58 mpdm->count--;
60 free(v);
64 static void destroy_value(mpdm_t v)
65 /* destroys a value */
67 if (mpdm->threaded_delete) {
68 /* atomically enqueue this value */
69 mpdm_mutex_lock(mpdm->del_queue_mutex);
71 v->next = mpdm->del;
72 mpdm->del = v;
74 mpdm_mutex_unlock(mpdm->del_queue_mutex);
76 /* notify the del queue thread */
77 mpdm_semaphore_post(mpdm->del_queue_sem);
79 else
80 cleanup_value(v);
84 static mpdm_t del_queue_thread(mpdm_t args, mpdm_t ctxt)
85 /* delete queue processing thread */
87 for (;;) {
88 mpdm_t v;
90 /* wait for next value */
91 mpdm_semaphore_wait(mpdm->del_queue_sem);
93 /* atomically dequeue */
94 mpdm_mutex_lock(mpdm->del_queue_mutex);
96 v = mpdm->del;
97 mpdm->del = v->next;
99 mpdm_mutex_unlock(mpdm->del_queue_mutex);
101 cleanup_value(v);
107 * mpdm_new - Creates a new value.
108 * @flags: flags
109 * @data: pointer to real data
110 * @size: size of data
112 * Creates a new value. @flags is an or-ed set of flags, @data is a
113 * pointer to the data the value will store and @size the size of these
114 * data (if value is to be a multiple one, @size is a number of elements,
115 * or a number of bytes otherwise).
117 * This function is normally not directly used; use any of the type
118 * creation macros instead.
119 * [Value Creation]
121 mpdm_t mpdm_new(int flags, const void *data, int size)
123 mpdm_t v = NULL;
125 /* alloc */
126 if ((v = malloc(sizeof(struct mpdm_val))) != NULL) {
127 /* account one value more */
128 mpdm->count++;
130 v->flags = flags;
131 v->ref = 0;
132 v->data = data;
133 v->size = size;
134 v->next = NULL;
137 return v;
142 * mpdm_ref - Increments the reference count of a value.
143 * @v: the value
145 * Increments the reference count of a value.
146 * [Value Management]
148 mpdm_t mpdm_ref(mpdm_t v)
150 if (v != NULL)
151 v->ref++;
153 return v;
158 * mpdm_unref - Decrements the reference count of a value.
159 * @v: the value
161 * Decrements the reference count of a value. If the reference
162 * count of the value reaches 0, it's destroyed.
163 * [Value Management]
165 mpdm_t mpdm_unref(mpdm_t v)
167 if (v != NULL) {
168 v->ref--;
170 if (v->ref <= 0) {
171 destroy_value(v);
172 v = NULL;
176 return v;
181 * mpdm_unrefnd - Decrements the reference count of a value, without destroy.
182 * @v: the value
184 * Decrements the reference count of a value, without destroying
185 * the value if it's unreferenced.
186 * [Value Management]
188 mpdm_t mpdm_unrefnd(mpdm_t v)
190 if (v != NULL)
191 v->ref--;
193 return v;
198 * mpdm_size - Returns the size of an element.
199 * @v: the element
201 * Returns the size of an element. It does not change the
202 * reference count of the value.
203 * [Value Management]
205 int mpdm_size(const mpdm_t v)
207 int r = 0;
209 /* NULL values have no size */
210 if (v != NULL)
211 r = v->size;
213 return r;
218 * mpdm_clone - Creates a clone of a value.
219 * @v: the value
221 * Creates a clone of a value. If the value is multiple, a new value will
222 * be created containing clones of all its elements; otherwise,
223 * the same unchanged value is returned.
224 * [Value Management]
226 mpdm_t mpdm_clone(const mpdm_t v)
228 mpdm_t r;
230 mpdm_ref(v);
232 if (MPDM_IS_ARRAY(v))
233 r = mpdm_aclone(v);
234 else
235 r = v;
237 mpdm_unref(v);
239 return r;
244 * mpdm_root - Returns the root hash.
246 * Returns the root hash. This hash is stored internally and can be used
247 * as a kind of global symbol table.
248 * [Value Management]
250 mpdm_t mpdm_root(void)
252 if (mpdm->root == NULL)
253 mpdm->root = mpdm_ref(MPDM_H(0));
255 return mpdm->root;
260 * mpdm_set_ival - Sets the integer value.
261 * @v: the value
262 * @ival: the integer
264 * Sets the integer value for @v. It does not change
265 * the reference count of @v.
267 mpdm_t mpdm_set_ival(mpdm_t v, int ival)
268 /* sets an integer value to a value */
270 v->flags |= MPDM_IVAL;
271 v->ival = ival;
273 return v;
278 * mpdm_set_rval - Sets the real value.
279 * @v: the value
280 * @rval: the real
282 * Sets the real value for @v. It does not change
283 * the reference count of @v.
285 mpdm_t mpdm_set_rval(mpdm_t v, double rval)
286 /* sets a real value to a value */
288 v->flags |= MPDM_RVAL;
289 v->rval = rval;
291 return v;
296 * mpdm_void - Refs then unrefs a value.
297 * @v: the value
299 * References and unreferences a value. To be used to receive
300 * the output of mpdm_exec() in case of it being void (i.e.
301 * its return value ignored).
303 void mpdm_void(mpdm_t v)
305 mpdm_ref(v);
306 mpdm_unref(v);
311 * mpdm_is_null - Returns 1 if a value is NULL.
312 * @v: the value
314 * Returns 1 if a value is NULL. The reference count is touched.
316 int mpdm_is_null(mpdm_t v)
318 int r;
320 mpdm_ref(v);
321 r = v == NULL ? 1 : 0;
322 mpdm_unref(v);
324 return r;
329 * mpdm_exec - Executes an executable value.
330 * @c: the code value
331 * @args: the arguments
332 * @ctxt: the context
334 * Executes an executable value. If @c is a scalar value, its data
335 * should be a pointer to a directly executable C function with a
336 * prototype of mpdm_t func(mpdm_t args, mpdm_t ctxt); if it's a multiple
337 * one, the first value's data should be a pointer to a directly executable
338 * C function with a prototype of
339 * mpdm_t func(mpdm_t b, mpdm_t args, mpdm_t ctxt) and
340 * the second value will be passed as the @b argument. This value is used
341 * to store bytecode or so when implementing virtual machines or compilers.
342 * The @ctxt is meant to be used as a special context to implement local
343 * symbol tables and such. Its meaning is free and can be NULL.
345 * Returns the return value of the code. If @c is NULL or not executable,
346 * returns NULL.
347 * [Value Management]
349 mpdm_t mpdm_exec(mpdm_t c, mpdm_t args, mpdm_t ctxt)
351 mpdm_t r = NULL;
353 mpdm_ref(c);
354 mpdm_ref(args);
355 mpdm_ref(ctxt);
357 if (c != NULL && (c->flags & MPDM_EXEC)) {
359 if (c->flags & MPDM_MULTIPLE) {
360 mpdm_t x;
361 mpdm_t(*func) (mpdm_t, mpdm_t, mpdm_t);
363 /* value is multiple; first element is the
364 3 argument version of the executable function,
365 next its optional additional information,
366 the arguments and the context */
367 x = mpdm_aget(c, 0);
369 if ((func =
370 (mpdm_t(*)(mpdm_t, mpdm_t, mpdm_t)) (x->data)) != NULL)
371 r = func(mpdm_aget(c, 1), args, ctxt);
373 else {
374 mpdm_t(*func) (mpdm_t, mpdm_t);
376 /* value is scalar; c is the 2 argument
377 version of the executable function */
378 if ((func = (mpdm_t(*)(mpdm_t, mpdm_t)) (c->data)) != NULL)
379 r = func(args, ctxt);
383 mpdm_unref(ctxt);
384 mpdm_unref(args);
385 mpdm_unref(c);
387 return r;
391 mpdm_t mpdm_exec_1(mpdm_t c, mpdm_t a1, mpdm_t ctxt)
393 mpdm_t r;
394 mpdm_t a = MPDM_A(1);
396 mpdm_ref(a);
397 mpdm_aset(a, a1, 0);
399 r = mpdm_exec(c, a, ctxt);
401 mpdm_unref(a);
403 return r;
407 mpdm_t mpdm_exec_2(mpdm_t c, mpdm_t a1, mpdm_t a2, mpdm_t ctxt)
409 mpdm_t r;
410 mpdm_t a = MPDM_A(2);
412 mpdm_ref(a);
413 mpdm_aset(a, a1, 0);
414 mpdm_aset(a, a2, 1);
416 r = mpdm_exec(c, a, ctxt);
418 mpdm_unref(a);
420 return r;
424 mpdm_t mpdm_exec_3(mpdm_t c, mpdm_t a1, mpdm_t a2, mpdm_t a3, mpdm_t ctxt)
426 mpdm_t r;
427 mpdm_t a = MPDM_A(3);
429 mpdm_ref(a);
430 mpdm_aset(a, a1, 0);
431 mpdm_aset(a, a2, 1);
432 mpdm_aset(a, a3, 2);
434 r = mpdm_exec(c, a, ctxt);
436 mpdm_unref(a);
438 return r;
442 mpdm_t mpdm_xnew(mpdm_t(*a1) (mpdm_t, mpdm_t, mpdm_t), mpdm_t a2)
444 mpdm_t x;
446 x = MPDM_A(2);
447 x->flags |= MPDM_EXEC;
449 mpdm_ref(x);
451 mpdm_aset(x, MPDM_X(a1), 0);
452 mpdm_aset(x, a2, 1);
454 mpdm_unrefnd(x);
456 return x;
460 mpdm_t mpdm_new_copy(int flags, void *ptr, int size)
462 mpdm_t r = NULL;
464 if (ptr != NULL) {
465 char *ptr2 = malloc(size);
466 memcpy(ptr2, ptr, size);
468 r = mpdm_new(MPDM_FREE | flags, ptr2, size);
471 return r;
475 static mpdm_t MPDM(const mpdm_t args, mpdm_t ctxt)
476 /* accesor / mutator for MPDM internal data */
478 mpdm_t v;
480 mpdm_ref(args);
482 v = mpdm_aget(args, 0);
484 if (v != NULL) {
485 mpdm_t w;
487 /* do changes */
488 if ((w = mpdm_hget_s(v, L"hash_buckets")) != NULL)
489 mpdm->hash_buckets = mpdm_ival(w);
490 else
491 if ((w = mpdm_hget_s(v, L"threaded_delete")) != NULL)
492 mpdm->threaded_delete = mpdm_ival(w);
495 /* now collect all information */
496 v = MPDM_H(0);
498 mpdm_ref(v);
500 mpdm_hset_s(v, L"version", MPDM_MBS(VERSION));
501 mpdm_hset_s(v, L"count", MPDM_I(mpdm->count));
502 mpdm_hset_s(v, L"hash_buckets", MPDM_I(mpdm->hash_buckets));
503 mpdm_hset_s(v, L"threaded_delete", MPDM_I(mpdm->threaded_delete));
505 mpdm_unref(args);
507 mpdm_unrefnd(v);
509 return v;
512 extern char **environ;
514 static mpdm_t build_env(void)
515 /* builds a hash with the environment */
517 char **ptr;
518 mpdm_t e = MPDM_H(0);
520 mpdm_ref(e);
522 for (ptr = environ; *ptr != NULL; ptr++) {
523 char *eq = strchr(*ptr, '=');
525 if (eq != NULL) {
526 mpdm_t k, v;
528 k = MPDM_NMBS((*ptr), eq - (*ptr));
529 v = MPDM_MBS(eq + 1);
531 mpdm_hset(e, k, v);
535 mpdm_unrefnd(e);
537 return e;
542 * mpdm_startup - Initializes MPDM.
544 * Initializes the Minimum Profit Data Manager. Returns 0 if
545 * everything went OK.
547 int mpdm_startup(void)
549 /* do the startup only unless done beforehand */
550 if (mpdm == NULL) {
551 /* alloc space */
552 if ((mpdm = malloc(sizeof(struct mpdm_control))) == NULL)
553 return -1;
555 /* cleans it */
556 memset(mpdm, '\0', sizeof(struct mpdm_control));
558 /* sets the defaults */
559 mpdm->hash_buckets = 31;
561 /* sets the threaded delete control */
562 mpdm->threaded_delete = 0;
564 /* create the threaded delete thread and control */
565 mpdm->del_queue_mutex = mpdm_new_mutex();
566 mpdm->del_queue_sem = mpdm_new_semaphore(0);
567 mpdm_exec_thread(MPDM_X(del_queue_thread), NULL, NULL);
569 /* sets the locale */
570 if (setlocale(LC_ALL, "") == NULL)
571 setlocale(LC_ALL, "C");
573 mpdm_encoding(NULL);
575 /* store the MPDM() function */
576 mpdm_hset_s(mpdm_root(), L"MPDM", MPDM_X(MPDM));
578 /* store the ENV hash */
579 mpdm_hset_s(mpdm_root(), L"ENV", build_env());
582 /* everything went OK */
583 return 0;
588 * mpdm_shutdown - Shuts down MPDM.
590 * Shuts down MPDM. No MPDM functions should be used from now on.
592 void mpdm_shutdown(void)
594 /* dummy, by now */
598 * MPDM_A - Creates an array value.
599 * @n: Number of elements
601 * Creates a new array value with @n elements.
602 * [Value Creation]
604 /** mpdm_t MPDM_A(int n); */
605 /* ; */
608 * MPDM_H - Creates a hash value.
609 * @n: Number of buckets in the hash (0: use default)
611 * Creates a new hash value with @n buckets. The number
612 * of buckets must be a prime number. If @n is 0, an
613 * optimal number of buckets will be used.
614 * [Value Creation]
616 /** mpdm_t MPDM_H(int n); */
617 /* ; */
620 * MPDM_LS - Creates a string value from a literal string.
621 * @wcs: the wide character string
623 * Creates a new string value from a literal, wide character string.
624 * A pointer to the string will be stored in the value (not a copy).
625 * [Value Creation]
627 /** mpdm_t MPDM_LS(wchar_t * wcs); */
628 /* ; */
631 * MPDM_S - Creates a string value from a string.
632 * @wcs: the wide character string
634 * Creates a new string value from a wide character string. The value
635 * will store a copy of the string that will be freed on destruction.
636 * [Value Creation]
638 /** mpdm_t MPDM_S(wchar_t * wcs); */
639 /* ; */
642 * MPDM_NS - Creates a string value from a string, with size.
643 * @wcs: the wide character string
644 * @s: the size in chars the string will hold
646 * Creates a new string value with a copy of the first @s characters
647 * from the @wcs string.
648 * [Value Creation]
650 /** mpdm_t MPDM_NS(wchar_t * wcs, int s); */
651 /* ; */
654 * MPDM_ENS - Creates a string value from an external string, with size.
655 * @wcs: the external wide character string
656 * @s: the size in chars the string will hold
658 * Creates a new string value with size @s. The @wcs string must be
659 * a dynamic value (i.e. allocated by malloc()) that will be freed on
660 * destruction.
661 * [Value Creation]
663 /** mpdm_t MPDM_ENS(wchar_t * wcs, int s); */
664 /* ; */
667 * MPDM_I - Creates an integer value.
668 * @i: the integer
670 * Creates a new integer value. MPDM integers are strings.
671 * [Value Creation]
673 /** mpdm_t MPDM_I(int i); */
674 /* ; */
677 * MPDM_R - Creates a real value.
678 * @r: the real number
680 * Creates a new real value. MPDM integers are strings.
681 * [Value Creation]
683 /** mpdm_t MPDM_R(double r); */
684 /* ; */
687 * MPDM_F - Creates a file value.
688 * @f: the file descriptor
690 * Creates a new file value.
691 * [Value Creation]
693 /** mpdm_t MPDM_F(FILE * f); */
694 /* ; */
697 * MPDM_MBS - Creates a string value from a multibyte string.
698 * @mbs: the multibyte string
700 * Creates a new string value from a multibyte string, that will be
701 * converted to wcs by mpdm_mbstowcs().
702 * [Value Creation]
704 /** mpdm_t MPDM_MBS(char * mbs); */
705 /* ; */
708 * MPDM_NMBS - Creates a string value from a multibyte string, with size.
709 * @mbs: the multibyte string
710 * @s: the size
712 * Creates a new string value with the first @s characters from the @mbs
713 * multibyte string, that will be converted to wcs by mpdm_mbstowcs().
714 * [Value Creation]
716 /** mpdm_t MPDM_NMBS(char * mbs, int s); */
717 /* ; */
720 * MPDM_2MBS - Creates a multibyte string value from a wide char string.
721 * @wcs: the wide char string
723 * Creates a multibyte string value from the @wcs wide char string,
724 * converting it by mpdm_wcstombs(). Take note that multibyte string values
725 * are not properly strings, so they cannot be used for string comparison
726 * and such.
727 * [Value Creation]
729 /** mpdm_t MPDM_2MBS(wchar_t * wcs); */
730 /* ; */
733 * MPDM_X - Creates a new executable value.
734 * @func: the C code function
736 * Creates a new executable value given a pointer to the @func C code function.
737 * The function must receive an mpdm_t array value (that will hold their
738 * arguments) and return another one.
739 * [Value Creation]
741 /** mpdm_t MPDM_X(mpdm_t (* func)(mpdm_t args)); */
742 /* ; */
745 * MPDM_C - Creates a new value with a copy of a buffer.
746 * @flags: additional flags
747 * @ptr: pointer to data
748 * @size: data size
750 * Create a new value with a copy of a buffer. The value will store a copy
751 * of @ptr and have the additional @flags.
752 * [Value Creation]
754 /** mpdm_t MPDM_C(int flags, void *ptr, int size); */
755 /* ; */
758 * MPDM_IS_ARRAY - Tests if a value is an array.
759 * @v: the value
761 * Returns non-zero if @v is an array.
763 /** int MPDM_IS_ARRAY(mpdm_t v); */
764 /* ; */
767 * MPDM_IS_HASH - Tests if a value is a hash.
768 * @v: the value
770 * Returns non-zero if @v is a hash.
772 /** int MPDM_IS_HASH(mpdm_t v); */
773 /* ; */
776 * MPDM_IS_EXEC - Tests if a value is executable.
777 * @v: the value
779 * Returns non-zero if @v is executable.
781 /** int MPDM_IS_EXEC(mpdm_t v); */
782 /* ; */
785 * MPDM_IS_STRING - Tests if a value is a string.
786 * @v: the value
788 * Returns non-zero if @v is a string.
790 /** int MPDM_IS_STRING(mpdm_t v); */
791 /* ; */