ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/memory.c
Revision: 1646
Committed: Wed Nov 7 21:02:43 2012 UTC (12 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 2463 byte(s)
Log Message:
- First pass of conf parser stabilization/cleanup

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * memory.c: Memory utilities.
4     *
5     * Copyright (C) 2002 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22 knight 31 * $Id$
23 adx 30 */
24    
25    
26     #include "stdinc.h"
27     #include "ircd_defs.h"
28     #include "irc_string.h"
29     #include "memory.h"
30 michael 1309 #include "log.h"
31 adx 30 #include "restart.h"
32    
33    
34     /*
35     * MyMalloc - allocate memory, call outofmemory on failure
36     */
37     void *
38     MyMalloc(size_t size)
39     {
40     void *ret = calloc(1, size);
41    
42     if (ret == NULL)
43     outofmemory();
44 michael 1011
45     return ret;
46 adx 30 }
47    
48     /*
49     * MyRealloc - reallocate memory, call outofmemory on failure
50     */
51     void *
52     MyRealloc(void *x, size_t y)
53     {
54     void *ret = realloc(x, y);
55    
56     if (ret == NULL)
57     outofmemory();
58 michael 1011
59     return ret;
60 adx 30 }
61    
62     void
63     MyFree(void *x)
64     {
65     if (x)
66     free(x);
67     }
68    
69 michael 1646 void *
70     xstrdup(const char *s)
71 adx 30 {
72 michael 1646 void *ret = malloc(strlen(s) + 1);
73    
74     if (ret == NULL)
75     outofmemory();
76    
77     strcpy(ret, s);
78    
79     return ret;
80 adx 30 }
81    
82 michael 1646 void *
83     xstrndup(const char *s, size_t len)
84     {
85     void *ret = malloc(len + 1);
86    
87     if (ret == NULL)
88     outofmemory();
89    
90     strlcpy(ret, s, len + 1);
91    
92     return ret;
93     }
94    
95 adx 30 /* outofmemory()
96     *
97     * input - NONE
98     * output - NONE
99     * side effects - simply try to report there is a problem.
100     * Abort if it was called more than once
101     */
102     void
103     outofmemory(void)
104     {
105     static int was_here = 0;
106    
107 michael 1011 if (was_here++)
108 adx 30 abort();
109    
110 michael 1247 ilog(LOG_TYPE_IRCD, "Out of memory: restarting server...");
111 adx 30 restart("Out of Memory");
112     }
113 michael 1011
114     #ifndef NDEBUG
115     /*
116     * frob some memory. debugging time.
117     * -- adrian
118     */
119     void
120     mem_frob(void *data, int len)
121     {
122     /* correct for Intel only! little endian */
123     unsigned char b[4] = { 0xef, 0xbe, 0xad, 0xde };
124     int i;
125     char *cdata = data;
126    
127     for (i = 0; i < len; i++)
128     {
129     *cdata = b[i % 4];
130     cdata++;
131     }
132     }
133     #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision