]> asedeno.scripts.mit.edu Git - pssh.git/blob - util/xmalloc.c
My 2006-07-10 release.
[pssh.git] / util / xmalloc.c
1 /**********
2  * Copyright (c) 2003-2005 Greg Parker.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY GREG PARKER ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  **********/
24
25 #include "includes.h"
26 #include "ssh/ssh.h"
27
28 #include <PalmOSGlue.h>
29
30 // #define DEBUG_ARENA
31 // #define DEBUG_NONARENA
32
33 // arena allocator is intended for use if pssh ever supports multiple 
34 // simultaneous connections. An error in one connection would trigger 
35 // destruction of its arena during cleanup.
36 // Currently, arena and non-arena allocators are identical.
37
38 void *arena_malloc(size_t s) 
39 {
40     void *p;
41     if (s == 0) s = 1; // fixme openssh is expecting fatal for size 0
42     p = MemGluePtrNew(s);
43     if (p == NULL) {
44         debug_printf("%ld bytes", s);
45         fatal("arena_malloc: out of memory");
46     }
47     return p;
48 }
49
50 void arena_free(void *p) 
51 {
52     if (!p) return; // needed by PuTTY?
53     MemPtrFree(p);
54 }
55
56 void *arena_calloc(size_t s) 
57 {
58     void *p;
59     p = arena_malloc(s);
60     memset(p, 0, s);
61     return p;
62 }
63
64 void *arena_realloc(void *oldp, size_t newsize)
65 {
66     void *newp;
67
68     newp = arena_malloc(newsize);
69     if (!oldp) {
70         return newp;
71     } else {
72         size_t oldsize;
73         oldsize = MemPtrSize(oldp);
74         memcpy(newp, oldp, MIN(newsize, oldsize));
75         arena_free(oldp); // fixme reallocf?
76         return newp;
77     }
78 }
79
80 char *arena_strdup(const char *s)
81 {
82     size_t len = 1+strlen(s);
83     char *news;
84     news = arena_malloc(len);
85     memcpy(news, s, len);
86     return news;
87 }
88