Integrate cctools-751 changes
[striptease.git] / libstuff / allocate.c
blob318fba4aabbcd0cec3fabf33468c44f9a4a981c3
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <mach/mach.h>
28 #include "stuff/allocate.h"
29 #include "stuff/errors.h"
31 * allocate() is just a wrapper around malloc that prints an error message and
32 * exits if the malloc fails.
34 __private_extern__
35 void *
36 allocate(
37 size_t size)
39 void *p;
41 if(size == 0)
42 return(NULL);
43 if((p = malloc(size)) == NULL)
44 system_fatal("virtual memory exhausted (malloc failed)");
45 return(p);
49 * reallocate() is just a wrapper around realloc that prints and error message
50 * and exits if the realloc fails.
52 __private_extern__
53 void *
54 reallocate(
55 void *p,
56 size_t size)
58 if(p == NULL)
59 return(allocate(size));
60 if((p = realloc(p, size)) == NULL)
61 system_fatal("virtual memory exhausted (realloc failed)");
62 return(p);
66 * savestr() malloc's space for the string passed to it, copys the string into
67 * the space and returns a pointer to that space.
69 __private_extern__
70 char *
71 savestr(
72 const char *s)
74 long len;
75 char *r;
77 len = strlen(s) + 1;
78 r = (char *)allocate(len);
79 strcpy(r, s);
80 return(r);
84 * Makestr() creates a string that is the concatenation of a variable number of
85 * strings. It is pass a variable number of pointers to strings and the last
86 * pointer is NULL. It returns the pointer to the string it created. The
87 * storage for the string is malloc()'ed can be free()'ed when nolonger needed.
89 __private_extern__
90 char *
91 makestr(
92 const char *args,
93 ...)
95 va_list ap;
96 char *s, *p;
97 long size;
99 size = 0;
100 if(args != NULL){
101 size += strlen(args);
102 va_start(ap, args);
103 p = (char *)va_arg(ap, char *);
104 while(p != NULL){
105 size += strlen(p);
106 p = (char *)va_arg(ap, char *);
109 s = allocate(size + 1);
110 *s = '\0';
112 if(args != NULL){
113 (void)strcat(s, args);
114 va_start(ap, args);
115 p = (char *)va_arg(ap, char *);
116 while(p != NULL){
117 (void)strcat(s, p);
118 p = (char *)va_arg(ap, char *);
120 va_end(ap);
122 return(s);