d8de25732ad46789545cbf5224e7ed04100a6f21
[ir-tcl-moved-to-github.git] / mem.c
1 /*
2  * IR toolkit for tcl/tk
3  * (c) Index Data 1995
4  * See the file LICENSE for details.
5  * Sebastian Hammer, Adam Dickmeiss
6  *
7  * $Log: mem.c,v $
8  * Revision 1.3  1996-07-03 13:31:14  adam
9  * The xmalloc/xfree functions from YAZ are used to manage memory.
10  *
11  * Revision 1.2  1995/08/29  15:30:15  adam
12  * Work on GRS records.
13  *
14  * Revision 1.1  1995/08/04  11:32:40  adam
15  * More work on output queue. Memory related routines moved
16  * to mem.c
17  *
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <assert.h>
24
25 #include "ir-tclp.h"
26
27 /*
28  * ir_tcl_malloc: Allocate n byte from the heap
29  */
30 void *ir_tcl_malloc (size_t n)
31 {
32     void *p = xmalloc (n);
33     if (!p)
34     {
35         logf (LOG_FATAL, "Out of memory. %ld bytes requested", (long) n);
36         exit (1);
37     }
38     return p;
39 }
40
41 /*
42  * ir_tcl_strdup: Duplicate string
43  */
44 int ir_tcl_strdup (Tcl_Interp *interp, char** p, const char *s)
45 {
46     size_t len;
47
48     if (!s)
49     {
50         *p = NULL;
51         return TCL_OK;
52     }
53     len = strlen(s)+1;
54     *p = xmalloc (len);
55     if (!*p)
56     {
57         if (!interp) 
58         {
59             logf (LOG_FATAL, "Out of memory in strdup. %ld bytes", len);
60             exit (1);
61         }
62         interp->result = "strdup fail";
63         return TCL_ERROR;
64     }
65     strcpy (*p, s);
66     return TCL_OK;
67 }
68
69 /*
70  * ir_strdel: Delete string
71  */
72 int ir_tcl_strdel (Tcl_Interp *interp, char **p)
73 {
74     xfree (*p);
75     *p = NULL;
76     return TCL_OK;
77 }
78