Work on GRS records.
[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.2  1995-08-29 15:30:15  adam
9  * Work on GRS records.
10  *
11  * Revision 1.1  1995/08/04  11:32:40  adam
12  * More work on output queue. Memory related routines moved
13  * to mem.c
14  *
15  */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <assert.h>
21
22 #include "ir-tclp.h"
23
24 /*
25  * ir_tcl_malloc: Allocate n byte from the heap
26  */
27 void *ir_tcl_malloc (size_t n)
28 {
29     void *p = malloc (n);
30     if (!p)
31     {
32         logf (LOG_FATAL, "Out of memory. %ld bytes requested", (long) n);
33         exit (1);
34     }
35     return p;
36 }
37
38 /*
39  * ir_tcl_strdup: Duplicate string
40  */
41 int ir_tcl_strdup (Tcl_Interp *interp, char** p, const char *s)
42 {
43     size_t len;
44
45     if (!s)
46     {
47         *p = NULL;
48         return TCL_OK;
49     }
50     len = strlen(s)+1;
51     *p = malloc (len);
52     if (!*p)
53     {
54         if (!interp) 
55         {
56             logf (LOG_FATAL, "Out of memory in strdup. %ld bytes", len);
57             exit (1);
58         }
59         interp->result = "strdup fail";
60         return TCL_ERROR;
61     }
62     strcpy (*p, s);
63     return TCL_OK;
64 }
65
66 /*
67  * ir_strdel: Delete string
68  */
69 int ir_tcl_strdel (Tcl_Interp *interp, char **p)
70 {
71     free (*p);
72     *p = NULL;
73     return TCL_OK;
74 }
75