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
) {
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
);
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
);
78 mpdm_mutex_unlock(mpdm
->del_queue_mutex
);
80 /* notify the del queue thread */
81 mpdm_semaphore_post(mpdm
->del_queue_sem
);
88 static mpdm_t
del_queue_thread(mpdm_t args
, mpdm_t ctxt
)
89 /* delete queue processing thread */
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
)
101 /* atomically dequeue */
102 mpdm_mutex_lock(mpdm
->del_queue_mutex
);
107 mpdm_mutex_unlock(mpdm
->del_queue_mutex
);
115 * mpdm_new - Creates a new value.
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.
129 mpdm_t
mpdm_new(int flags
, const void *data
, int size
)
134 if ((v
= malloc(sizeof(struct mpdm_val
))) != NULL
) {
135 /* account one value more */
150 * mpdm_ref - Increments the reference count of a value.
153 * Increments the reference count of a value.
156 mpdm_t
mpdm_ref(mpdm_t v
)
166 * mpdm_unref - Decrements the reference count of a value.
169 * Decrements the reference count of a value. If the reference
170 * count of the value reaches 0, it's destroyed.
173 mpdm_t
mpdm_unref(mpdm_t v
)
189 * mpdm_unrefnd - Decrements the reference count of a value, without destroy.
192 * Decrements the reference count of a value, without destroying
193 * the value if it's unreferenced.
196 mpdm_t
mpdm_unrefnd(mpdm_t v
)
206 * mpdm_size - Returns the size of an element.
209 * Returns the size of an element. It does not change the
210 * reference count of the value.
213 int mpdm_size(const mpdm_t v
)
217 /* NULL values have no size */
226 * mpdm_clone - Creates a clone of a 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.
234 mpdm_t
mpdm_clone(const mpdm_t v
)
240 if (MPDM_IS_ARRAY(v
))
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.
258 mpdm_t
mpdm_root(void)
260 if (mpdm
->root
== NULL
)
261 mpdm
->root
= mpdm_ref(MPDM_H(0));
268 * mpdm_set_ival - Sets the integer value.
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
;
286 * mpdm_set_rval - Sets the real value.
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
;
304 * mpdm_void - Refs then unrefs a 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
)
319 * mpdm_is_null - Returns 1 if a value is NULL.
322 * Returns 1 if a value is NULL. The reference count is touched.
324 int mpdm_is_null(mpdm_t v
)
329 r
= v
== NULL
? 1 : 0;
337 * mpdm_exec - Executes an executable value.
339 * @args: the arguments
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,
357 mpdm_t
mpdm_exec(mpdm_t c
, mpdm_t args
, mpdm_t ctxt
)
365 if (c
!= NULL
&& (c
->flags
& MPDM_EXEC
)) {
367 if (c
->flags
& MPDM_MULTIPLE
) {
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 */
378 (mpdm_t(*)(mpdm_t
, mpdm_t
, mpdm_t
)) (x
->data
)) != NULL
)
379 r
= func(mpdm_aget(c
, 1), args
, ctxt
);
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
);
399 mpdm_t
mpdm_exec_1(mpdm_t c
, mpdm_t a1
, mpdm_t ctxt
)
402 mpdm_t a
= MPDM_A(1);
407 r
= mpdm_exec(c
, a
, ctxt
);
415 mpdm_t
mpdm_exec_2(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t ctxt
)
418 mpdm_t a
= MPDM_A(2);
424 r
= mpdm_exec(c
, a
, ctxt
);
432 mpdm_t
mpdm_exec_3(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t a3
, mpdm_t ctxt
)
435 mpdm_t a
= MPDM_A(3);
442 r
= mpdm_exec(c
, a
, ctxt
);
450 mpdm_t
mpdm_xnew(mpdm_t(*a1
) (mpdm_t
, mpdm_t
, mpdm_t
), mpdm_t a2
)
455 x
->flags
|= MPDM_EXEC
;
459 mpdm_aset(x
, MPDM_X(a1
), 0);
468 mpdm_t
mpdm_new_copy(int flags
, void *ptr
, int size
)
473 char *ptr2
= malloc(size
);
474 memcpy(ptr2
, ptr
, size
);
476 r
= mpdm_new(MPDM_FREE
| flags
, ptr2
, size
);
483 static mpdm_t
MPDM(const mpdm_t args
, mpdm_t ctxt
)
484 /* accesor / mutator for MPDM internal data */
490 v
= mpdm_aget(args
, 0);
496 if ((w
= mpdm_hget_s(v
, L
"hash_buckets")) != NULL
)
497 mpdm
->hash_buckets
= mpdm_ival(w
);
499 if ((w
= mpdm_hget_s(v
, L
"threaded_delete")) != NULL
)
500 mpdm
->threaded_delete
= mpdm_ival(w
);
503 /* now collect all information */
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
));
520 extern char **environ
;
522 static mpdm_t
build_env(void)
523 /* builds a hash with the environment */
526 mpdm_t e
= MPDM_H(0);
530 for (ptr
= environ
; *ptr
!= NULL
; ptr
++) {
531 char *eq
= strchr(*ptr
, '=');
536 k
= MPDM_NMBS((*ptr
), eq
- (*ptr
));
537 v
= MPDM_MBS(eq
+ 1);
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 */
560 if ((mpdm
= malloc(sizeof(struct mpdm_control
))) == NULL
)
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");
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 */
596 * mpdm_shutdown - Shuts down MPDM.
598 * Shuts down MPDM. No MPDM functions should be used from now on.
600 void mpdm_shutdown(void)
606 * MPDM_A - Creates an array value.
607 * @n: Number of elements
609 * Creates a new array value with @n elements.
612 /** mpdm_t MPDM_A(int n); */
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.
624 /** mpdm_t MPDM_H(int n); */
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).
635 /** mpdm_t MPDM_LS(wchar_t * wcs); */
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.
646 /** mpdm_t MPDM_S(wchar_t * wcs); */
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.
658 /** mpdm_t MPDM_NS(wchar_t * wcs, int s); */
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
671 /** mpdm_t MPDM_ENS(wchar_t * wcs, int s); */
675 * MPDM_I - Creates an integer value.
678 * Creates a new integer value. MPDM integers are strings.
681 /** mpdm_t MPDM_I(int i); */
685 * MPDM_R - Creates a real value.
686 * @r: the real number
688 * Creates a new real value. MPDM integers are strings.
691 /** mpdm_t MPDM_R(double r); */
695 * MPDM_F - Creates a file value.
696 * @f: the file descriptor
698 * Creates a new file value.
701 /** mpdm_t MPDM_F(FILE * f); */
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().
712 /** mpdm_t MPDM_MBS(char * mbs); */
716 * MPDM_NMBS - Creates a string value from a multibyte string, with size.
717 * @mbs: the multibyte string
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().
724 /** mpdm_t MPDM_NMBS(char * mbs, int s); */
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
737 /** mpdm_t MPDM_2MBS(wchar_t * wcs); */
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.
749 /** mpdm_t MPDM_X(mpdm_t (* func)(mpdm_t args)); */
753 * MPDM_C - Creates a new value with a copy of a buffer.
754 * @flags: additional flags
755 * @ptr: pointer to data
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.
762 /** mpdm_t MPDM_C(int flags, void *ptr, int size); */
766 * MPDM_IS_ARRAY - Tests if a value is an array.
769 * Returns non-zero if @v is an array.
771 /** int MPDM_IS_ARRAY(mpdm_t v); */
775 * MPDM_IS_HASH - Tests if a value is a hash.
778 * Returns non-zero if @v is a hash.
780 /** int MPDM_IS_HASH(mpdm_t v); */
784 * MPDM_IS_EXEC - Tests if a value is executable.
787 * Returns non-zero if @v is executable.
789 /** int MPDM_IS_EXEC(mpdm_t v); */
793 * MPDM_IS_STRING - Tests if a value is a string.
796 * Returns non-zero if @v is a string.
798 /** int MPDM_IS_STRING(mpdm_t v); */