]> git.tuebingen.mpg.de Git - osl.git/blob - string.c
7ac77d0f0dc97445b21ea23ccb6910619af54f6c
[osl.git] / string.c
1 /*
2  * Copyright (C) 2004-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file string.c Memory allocation and string handling functions. */
8
9 #include "log.h"
10 #include "string.h"
11
12 #include <sys/time.h> /* gettimeofday */
13 #include <pwd.h>
14 #include <sys/utsname.h> /* uname() */
15 #include <string.h>
16
17 #include "error.h"
18
19 /**
20  * Paraslash's version of malloc().
21  *
22  * \param size The desired new size.
23  *
24  * A wrapper for malloc(3) which exits on errors.
25  *
26  * \return A pointer to the allocated memory, which is suitably aligned for any
27  * kind of variable.
28  *
29  * \sa malloc(3).
30  */
31 __must_check __malloc void *para_malloc(size_t size)
32 {
33         assert(size);
34         void *p = malloc(size);
35
36         if (!p) {
37                 EMERG_LOG("malloc failed (size = %zu),  aborting\n",
38                         size);
39                 exit(EXIT_FAILURE);
40         }
41         return p;
42 }
43
44 /**
45  * Paraslash's version of calloc().
46  *
47  * \param size The desired new size.
48  *
49  * A wrapper for calloc(3) which exits on errors.
50  *
51  * \return A pointer to the allocated and zeroed-out memory, which is suitably
52  * aligned for any kind of variable.
53  *
54  * \sa calloc(3)
55  */
56 __must_check __malloc void *para_calloc(size_t size)
57 {
58         void *ret = para_malloc(size);
59
60         memset(ret, 0, size);
61         return ret;
62 }
63
64 /**
65  * Paraslash's version of strdup().
66  *
67  * \param s The string to be duplicated.
68  *
69  * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
70  * there is no need to check the return value in the caller.
71  *
72  * \return A pointer to the duplicated string. If \p s was the NULL pointer,
73  * an pointer to an empty string is returned.
74  *
75  * \sa strdup(3)
76  */
77 __must_check __malloc char *para_strdup(const char *s)
78 {
79         char *ret;
80
81         if ((ret = strdup(s? s: "")))
82                 return ret;
83         EMERG_LOG("strdup failed, aborting\n");
84         exit(EXIT_FAILURE);
85 }