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
38 /* control structure */
40 struct mpdm_control
*mpdm
= NULL
;
45 static void cleanup_value(mpdm_t v
)
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
);
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
);
74 mpdm_mutex_unlock(mpdm
->del_queue_mutex
);
76 /* notify the del queue thread */
77 mpdm_semaphore_post(mpdm
->del_queue_sem
);
84 static mpdm_t
del_queue_thread(mpdm_t args
, mpdm_t ctxt
)
85 /* delete queue processing thread */
90 /* wait for next value */
91 mpdm_semaphore_wait(mpdm
->del_queue_sem
);
93 /* atomically dequeue */
94 mpdm_mutex_lock(mpdm
->del_queue_mutex
);
99 mpdm_mutex_unlock(mpdm
->del_queue_mutex
);
107 * mpdm_new - Creates a new value.
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.
121 mpdm_t
mpdm_new(int flags
, const void *data
, int size
)
126 if ((v
= malloc(sizeof(struct mpdm_val
))) != NULL
) {
127 /* account one value more */
142 * mpdm_ref - Increments the reference count of a value.
145 * Increments the reference count of a value.
148 mpdm_t
mpdm_ref(mpdm_t v
)
158 * mpdm_unref - Decrements the reference count of a value.
161 * Decrements the reference count of a value. If the reference
162 * count of the value reaches 0, it's destroyed.
165 mpdm_t
mpdm_unref(mpdm_t v
)
181 * mpdm_unrefnd - Decrements the reference count of a value, without destroy.
184 * Decrements the reference count of a value, without destroying
185 * the value if it's unreferenced.
188 mpdm_t
mpdm_unrefnd(mpdm_t v
)
198 * mpdm_size - Returns the size of an element.
201 * Returns the size of an element. It does not change the
202 * reference count of the value.
205 int mpdm_size(const mpdm_t v
)
209 /* NULL values have no size */
218 * mpdm_clone - Creates a clone of a 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.
226 mpdm_t
mpdm_clone(const mpdm_t v
)
232 if (MPDM_IS_ARRAY(v
))
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.
250 mpdm_t
mpdm_root(void)
252 if (mpdm
->root
== NULL
)
253 mpdm
->root
= mpdm_ref(MPDM_H(0));
260 * mpdm_set_ival - Sets the integer value.
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
;
278 * mpdm_set_rval - Sets the real value.
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
;
296 * mpdm_void - Refs then unrefs a 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
)
311 * mpdm_is_null - Returns 1 if a value is NULL.
314 * Returns 1 if a value is NULL. The reference count is touched.
316 int mpdm_is_null(mpdm_t v
)
321 r
= v
== NULL
? 1 : 0;
329 * mpdm_exec - Executes an executable value.
331 * @args: the arguments
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,
349 mpdm_t
mpdm_exec(mpdm_t c
, mpdm_t args
, mpdm_t ctxt
)
357 if (c
!= NULL
&& (c
->flags
& MPDM_EXEC
)) {
359 if (c
->flags
& MPDM_MULTIPLE
) {
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 */
370 (mpdm_t(*)(mpdm_t
, mpdm_t
, mpdm_t
)) (x
->data
)) != NULL
)
371 r
= func(mpdm_aget(c
, 1), args
, ctxt
);
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
);
391 mpdm_t
mpdm_exec_1(mpdm_t c
, mpdm_t a1
, mpdm_t ctxt
)
394 mpdm_t a
= MPDM_A(1);
399 r
= mpdm_exec(c
, a
, ctxt
);
407 mpdm_t
mpdm_exec_2(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t ctxt
)
410 mpdm_t a
= MPDM_A(2);
416 r
= mpdm_exec(c
, a
, ctxt
);
424 mpdm_t
mpdm_exec_3(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t a3
, mpdm_t ctxt
)
427 mpdm_t a
= MPDM_A(3);
434 r
= mpdm_exec(c
, a
, ctxt
);
442 mpdm_t
mpdm_xnew(mpdm_t(*a1
) (mpdm_t
, mpdm_t
, mpdm_t
), mpdm_t a2
)
447 x
->flags
|= MPDM_EXEC
;
451 mpdm_aset(x
, MPDM_X(a1
), 0);
460 mpdm_t
mpdm_new_copy(int flags
, void *ptr
, int size
)
465 char *ptr2
= malloc(size
);
466 memcpy(ptr2
, ptr
, size
);
468 r
= mpdm_new(MPDM_FREE
| flags
, ptr2
, size
);
475 static mpdm_t
MPDM(const mpdm_t args
, mpdm_t ctxt
)
476 /* accesor / mutator for MPDM internal data */
482 v
= mpdm_aget(args
, 0);
488 if ((w
= mpdm_hget_s(v
, L
"hash_buckets")) != NULL
)
489 mpdm
->hash_buckets
= mpdm_ival(w
);
491 if ((w
= mpdm_hget_s(v
, L
"threaded_delete")) != NULL
)
492 mpdm
->threaded_delete
= mpdm_ival(w
);
495 /* now collect all information */
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
));
512 extern char **environ
;
514 static mpdm_t
build_env(void)
515 /* builds a hash with the environment */
518 mpdm_t e
= MPDM_H(0);
522 for (ptr
= environ
; *ptr
!= NULL
; ptr
++) {
523 char *eq
= strchr(*ptr
, '=');
528 k
= MPDM_NMBS((*ptr
), eq
- (*ptr
));
529 v
= MPDM_MBS(eq
+ 1);
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 */
552 if ((mpdm
= malloc(sizeof(struct mpdm_control
))) == NULL
)
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");
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 */
588 * mpdm_shutdown - Shuts down MPDM.
590 * Shuts down MPDM. No MPDM functions should be used from now on.
592 void mpdm_shutdown(void)
598 * MPDM_A - Creates an array value.
599 * @n: Number of elements
601 * Creates a new array value with @n elements.
604 /** mpdm_t MPDM_A(int n); */
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.
616 /** mpdm_t MPDM_H(int n); */
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).
627 /** mpdm_t MPDM_LS(wchar_t * wcs); */
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.
638 /** mpdm_t MPDM_S(wchar_t * wcs); */
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.
650 /** mpdm_t MPDM_NS(wchar_t * wcs, int s); */
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
663 /** mpdm_t MPDM_ENS(wchar_t * wcs, int s); */
667 * MPDM_I - Creates an integer value.
670 * Creates a new integer value. MPDM integers are strings.
673 /** mpdm_t MPDM_I(int i); */
677 * MPDM_R - Creates a real value.
678 * @r: the real number
680 * Creates a new real value. MPDM integers are strings.
683 /** mpdm_t MPDM_R(double r); */
687 * MPDM_F - Creates a file value.
688 * @f: the file descriptor
690 * Creates a new file value.
693 /** mpdm_t MPDM_F(FILE * f); */
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().
704 /** mpdm_t MPDM_MBS(char * mbs); */
708 * MPDM_NMBS - Creates a string value from a multibyte string, with size.
709 * @mbs: the multibyte string
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().
716 /** mpdm_t MPDM_NMBS(char * mbs, int s); */
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
729 /** mpdm_t MPDM_2MBS(wchar_t * wcs); */
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.
741 /** mpdm_t MPDM_X(mpdm_t (* func)(mpdm_t args)); */
745 * MPDM_C - Creates a new value with a copy of a buffer.
746 * @flags: additional flags
747 * @ptr: pointer to data
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.
754 /** mpdm_t MPDM_C(int flags, void *ptr, int size); */
758 * MPDM_IS_ARRAY - Tests if a value is an array.
761 * Returns non-zero if @v is an array.
763 /** int MPDM_IS_ARRAY(mpdm_t v); */
767 * MPDM_IS_HASH - Tests if a value is a hash.
770 * Returns non-zero if @v is a hash.
772 /** int MPDM_IS_HASH(mpdm_t v); */
776 * MPDM_IS_EXEC - Tests if a value is executable.
779 * Returns non-zero if @v is executable.
781 /** int MPDM_IS_EXEC(mpdm_t v); */
785 * MPDM_IS_STRING - Tests if a value is a string.
788 * Returns non-zero if @v is a string.
790 /** int MPDM_IS_STRING(mpdm_t v); */