summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f2a1934)
raw | patch | inline | side by side (parent: f2a1934)
author | Christopher Li <git@chrisli.org> | |
Tue, 26 Apr 2005 19:00:58 +0000 (12:00 -0700) | ||
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | |
Tue, 26 Apr 2005 19:00:58 +0000 (12:00 -0700) |
Introduce xmalloc and xrealloc to die gracefully with a descriptive
message when out of memory, rather than taking a SIGSEGV.
Signed-off-by: Christopher Li<chrislgit@chrisli.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
message when out of memory, rather than taking a SIGSEGV.
Signed-off-by: Christopher Li<chrislgit@chrisli.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
18 files changed:
blob.c | patch | blob | history | |
cache.h | patch | blob | history | |
checkout-cache.c | patch | blob | history | |
commit-tree.c | patch | blob | history | |
commit.c | patch | blob | history | |
convert-cache.c | patch | blob | history | |
diff-tree.c | patch | blob | history | |
diff.c | patch | blob | history | |
http-pull.c | patch | blob | history | |
init-db.c | patch | blob | history | |
object.c | patch | blob | history | |
read-cache.c | patch | blob | history | |
sha1_file.c | patch | blob | history | |
show-files.c | patch | blob | history | |
strbuf.c | patch | blob | history | |
tree.c | patch | blob | history | |
update-cache.c | patch | blob | history | |
write-tree.c | patch | blob | history |
index f79f5abd00ba2eb0b25cf244e0b21b559b86e1d7..3d99b93f020d84c5410c2b1056f2d7446b647d1e 100644 (file)
--- a/blob.c
+++ b/blob.c
{
struct object *obj = lookup_object(sha1);
if (!obj) {
- struct blob *ret = malloc(sizeof(struct blob));
+ struct blob *ret = xmalloc(sizeof(struct blob));
memset(ret, 0, sizeof(struct blob));
created_object(sha1, &ret->object);
ret->object.type = blob_type;
index 4ef80c392adb19df21a779edab366f99b7850e54..3277d48708f885fa1b7cc56c9d16061c65a2eeb9 100644 (file)
--- a/cache.h
+++ b/cache.h
unsigned long *size,
unsigned char *tree_sha1_ret);
+static inline void *xmalloc(int size)
+{
+ void *ret = malloc(size);
+ if (!ret)
+ die("Out of memory, malloc failed");
+ return ret;
+}
+
+static inline void *xrealloc(void *ptr, int size)
+{
+ void *ret = realloc(ptr, size);
+ if (!ret)
+ die("Out of memory, realloc failed");
+ return ret;
+}
+
#endif /* CACHE_H */
diff --git a/checkout-cache.c b/checkout-cache.c
index d1661eb5f800a4db2e8934e87b710b94662a261a..a1ef9448c35fa76ea036faae1a789f0cb654198d 100644 (file)
--- a/checkout-cache.c
+++ b/checkout-cache.c
static void create_directories(const char *path)
{
int len = strlen(path);
- char *buf = malloc(len + 1);
+ char *buf = xmalloc(len + 1);
const char *slash = path;
while ((slash = strchr(slash+1, '/')) != NULL) {
diff --git a/commit-tree.c b/commit-tree.c
index f6e408756042868b368cf403393c5b6c96343cbb..23de13361944ad7ba7c5320cf7cdd04e81842c60 100644 (file)
--- a/commit-tree.c
+++ b/commit-tree.c
*/
static void init_buffer(char **bufp, unsigned int *sizep)
{
- char *buf = malloc(BLOCKING);
+ char *buf = xmalloc(BLOCKING);
*sizep = 0;
*bufp = buf;
}
buf = *bufp;
if (newsize > alloc) {
alloc = (newsize + 32767) & ~32767;
- buf = realloc(buf, alloc);
+ buf = xrealloc(buf, alloc);
*bufp = buf;
}
*sizep = newsize;
diff --git a/commit.c b/commit.c
index 9fbcbd33c7530275a31c701207a34c4bc462da05..3956c7ba961781f72b39c42368df1e76b2d035dd 100644 (file)
--- a/commit.c
+++ b/commit.c
{
struct object *obj = lookup_object(sha1);
if (!obj) {
- struct commit *ret = malloc(sizeof(struct commit));
+ struct commit *ret = xmalloc(sizeof(struct commit));
memset(ret, 0, sizeof(struct commit));
created_object(sha1, &ret->object);
ret->object.type = commit_type;
void commit_list_insert(struct commit *item, struct commit_list **list_p)
{
- struct commit_list *new_list = malloc(sizeof(struct commit_list));
+ struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
new_list->item = item;
new_list->next = *list_p;
*list_p = new_list;
diff --git a/convert-cache.c b/convert-cache.c
index 7102e455163563894448fd7090d2409e379ed314..631d1aa910e7328c99642495f93908c749074f91 100644 (file)
--- a/convert-cache.c
+++ b/convert-cache.c
static struct entry *insert_new(unsigned char *sha1, int pos)
{
- struct entry *new = malloc(sizeof(struct entry));
-
+ struct entry *new = xmalloc(sizeof(struct entry));
memset(new, 0, sizeof(*new));
memcpy(new->old_sha1, sha1, 20);
memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
{
- char *new = malloc(size);
+ char *new = xmalloc(size);
unsigned long newlen = 0;
unsigned long used;
static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
{
- char *new = malloc(size + 100);
+ char *new = xmalloc(size + 100);
unsigned long newlen = 0;
-
+
// "tree <sha1>\n"
memcpy(new + newlen, buffer, 46);
newlen += 46;
if (!data)
die("unable to read object %s", sha1_to_hex(sha1));
- buffer = malloc(size);
+ buffer = xmalloc(size);
memcpy(buffer, data, size);
if (!strcmp(type, "blob")) {
diff --git a/diff-tree.c b/diff-tree.c
index 618fdb616cebbd2fc9f1cddc0b6b75fd575250a1..3579b5fd1182679a39b83eaaa9dd0e7c970f4545 100644 (file)
--- a/diff-tree.c
+++ b/diff-tree.c
static char *malloc_base(const char *base, const char *path, int pathlen)
{
int baselen = strlen(base);
- char *newbase = malloc(baselen + pathlen + 2);
+ char *newbase = xmalloc(baselen + pathlen + 2);
memcpy(newbase, base, baselen);
memcpy(newbase + baselen, path, pathlen);
memcpy(newbase + baselen + pathlen, "/", 2);
paths = &argv[3];
nr_paths = argc - 3;
- pathlens = malloc(nr_paths * sizeof(int));
+ pathlens = xmalloc(nr_paths * sizeof(int));
for (i=0; i<nr_paths; i++)
pathlens[i] = strlen(paths[i]);
}
index 9905cdb0122afecc282273c0e728c3f3b75fc51d..dd71d8346d0a2bd502fbfd3fd5b96711aab4ade4 100644 (file)
--- a/diff.c
+++ b/diff.c
if (*cp == '\'')
cnt += 3;
- if (! (buf = malloc(cnt)))
- return buf;
+ buf = xmalloc(cnt);
bp = buf;
while ((c = *src++)) {
if (c != '\'')
strlen(diff_arg) +
strlen(name_1_sq) + strlen(name_2_sq)
- 5);
- char *cmd = malloc(cmd_size);
+ char *cmd = xmalloc(cmd_size);
int next_at = 0;
next_at += snprintf(cmd+next_at, cmd_size-next_at,
diff --git a/http-pull.c b/http-pull.c
index a17225719c53508a37905618c624ad8c4d0372ec..192dcc370dee47c52c72915394bb6f2a79f64e12 100644 (file)
--- a/http-pull.c
+++ b/http-pull.c
curl_easy_setopt(curl, CURLOPT_FILE, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
- url = malloc(strlen(base) + 50);
+ url = xmalloc(strlen(base) + 50);
strcpy(url, base);
posn = url + strlen(base);
strcpy(posn, "objects/");
diff --git a/init-db.c b/init-db.c
index dad06351ca35d0d2f68cd9e719c49805386f96fa..83f95e8b926f4fd28e0db0ccfc4f040d4172ee8a 100644 (file)
--- a/init-db.c
+++ b/init-db.c
fprintf(stderr, "defaulting to local storage area\n");
}
len = strlen(sha1_dir);
- path = malloc(len + 40);
+ path = xmalloc(len + 40);
memcpy(path, sha1_dir, len);
safe_create_dir(sha1_dir);
diff --git a/object.c b/object.c
index cfa2337641aabd692c659eabef347be11af9f517..91bbc6e5e2eadfb0a66b14d992eac260d07267f8 100644 (file)
--- a/object.c
+++ b/object.c
if (obj_allocs == nr_objs) {
obj_allocs = alloc_nr(obj_allocs);
- objs = realloc(objs, obj_allocs * sizeof(struct object *));
+ objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
}
/* Insert it into the right place */
}
target->used = 1;
- p = malloc(sizeof(*p));
+ p = xmalloc(sizeof(*p));
p->item = target;
p->next = NULL;
*pp = p;
diff --git a/read-cache.c b/read-cache.c
index f67aceb6b12f6d9cadf6a80bb1d33cf1bcd7f619..2354e8039b80af1645863b881068f0cf26134fe0 100644 (file)
--- a/read-cache.c
+++ b/read-cache.c
/* Make sure the array is big enough .. */
if (active_nr == active_alloc) {
active_alloc = alloc_nr(active_alloc);
- active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
+ active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
}
/* Add it in.. */
diff --git a/sha1_file.c b/sha1_file.c
index d98b265bbbb6dbf222877a41bcde43eb3b5a9158..db2880e389e556dd3a5eef02aa8a3bb235528057 100644 (file)
--- a/sha1_file.c
+++ b/sha1_file.c
if (!base) {
char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
int len = strlen(sha1_file_directory);
- base = malloc(len + 60);
+ base = xmalloc(len + 60);
memcpy(base, sha1_file_directory, len);
memset(base+len, 0, 60);
base[len] = '/';
@@ -161,9 +161,7 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned l
return NULL;
bytes = strlen(buffer) + 1;
- buf = malloc(*size);
- if (!buf)
- return NULL;
+ buf = xmalloc(*size);
memcpy(buf, buffer + bytes, stream.total_out - bytes);
bytes = stream.total_out - bytes;
@@ -271,7 +269,7 @@ int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned cha
memset(&stream, 0, sizeof(stream));
deflateInit(&stream, Z_BEST_COMPRESSION);
size = deflateBound(&stream, len+hdrlen);
- compressed = malloc(size);
+ compressed = xmalloc(size);
/* Compress it */
stream.next_out = compressed;
diff --git a/show-files.c b/show-files.c
index b53ab1053e1acf7e2a1e9c97a4d87b76e8ee238e..afeb6356730e404320351022c29b6335bf366007 100644 (file)
--- a/show-files.c
+++ b/show-files.c
if (nr_dir == dir_alloc) {
dir_alloc = alloc_nr(dir_alloc);
- dir = realloc(dir, dir_alloc*sizeof(char *));
+ dir = xrealloc(dir, dir_alloc*sizeof(char *));
}
- name = malloc(len + 1);
+ name = xmalloc(len + 1);
memcpy(name, pathname, len + 1);
dir[nr_dir++] = name;
}
diff --git a/strbuf.c b/strbuf.c
index dac945c7c8ec3ca14791e472fd19a47cc07dcd15..d381c1d95bec4873bff07c97ebee9c4fc3f3d980 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
#include <stdio.h>
#include <stdlib.h>
#include "strbuf.h"
+#include "cache.h"
void strbuf_init(struct strbuf *sb) {
sb->buf = 0;
static void inline strbuf_add(struct strbuf *sb, int ch) {
if (sb->alloc <= sb->len) {
sb->alloc = sb->alloc * 3 / 2 + 16;
- sb->buf = realloc(sb->buf, sb->alloc);
+ sb->buf = xrealloc(sb->buf, sb->alloc);
}
sb->buf[sb->len++] = ch;
}
index 26b79270804cf401ccb1b292757169aa73569024..15a16d560619aee80e41bf816dd8474a3253b1a5 100644 (file)
--- a/tree.c
+++ b/tree.c
{
int len = strlen(pathname);
unsigned int size = cache_entry_size(baselen + len);
- struct cache_entry *ce = malloc(size);
+ struct cache_entry *ce = xmalloc(size);
memset(ce, 0, size);
if (S_ISDIR(mode)) {
int retval;
int pathlen = strlen(path);
- char *newbase = malloc(baselen + 1 + pathlen);
+ char *newbase = xmalloc(baselen + 1 + pathlen);
void *eltbuf;
char elttype[20];
unsigned long eltsize;
{
struct object *obj = lookup_object(sha1);
if (!obj) {
- struct tree *ret = malloc(sizeof(struct tree));
+ struct tree *ret = xmalloc(sizeof(struct tree));
memset(ret, 0, sizeof(struct tree));
created_object(sha1, &ret->object);
ret->object.type = tree_type;
sscanf(bufptr, "%o", &mode) != 1)
return -1;
- entry = malloc(sizeof(struct tree_entry_list));
+ entry = xmalloc(sizeof(struct tree_entry_list));
entry->name = strdup(path + 1);
entry->directory = S_ISDIR(mode);
entry->executable = mode & S_IXUSR;
diff --git a/update-cache.c b/update-cache.c
index e759c647a761c7cb5ca89ee4e15098f763e15793..16e1bb9aea6413db35039042289605124d759501 100644 (file)
--- a/update-cache.c
+++ b/update-cache.c
z_stream stream;
unsigned long size = st->st_size;
int max_out_bytes = size + 200;
- void *out = malloc(max_out_bytes);
- void *metadata = malloc(200);
+ void *out = xmalloc(max_out_bytes);
+ void *metadata = xmalloc(200);
int metadata_size;
void *in;
SHA_CTX c;
}
namelen = strlen(path);
size = cache_entry_size(namelen);
- ce = malloc(size);
+ ce = xmalloc(size);
memset(ce, 0, size);
memcpy(ce->name, path, namelen);
fill_stat_cache_info(ce, &st);
return ERR_PTR(-EINVAL);
size = ce_size(ce);
- updated = malloc(size);
+ updated = xmalloc(size);
memcpy(updated, ce, size);
fill_stat_cache_info(updated, &st);
return updated;
len = strlen(arg3);
size = cache_entry_size(len);
- ce = malloc(size);
+ ce = xmalloc(size);
memset(ce, 0, size);
memcpy(ce->sha1, sha1, 20);
diff --git a/write-tree.c b/write-tree.c
index bb7ceedb8e2d670f02880e0626b350a5a636f472..168352853d37bdca71d68ad8312b87b84477dea1 100644 (file)
--- a/write-tree.c
+++ b/write-tree.c
/* Guess at some random initial size */
size = 8192;
- buffer = malloc(size);
+ buffer = xmalloc(size);
offset = 0;
nr = 0;
entrylen = pathlen - baselen;
if (offset + entrylen + 100 > size) {
size = alloc_nr(offset + entrylen + 100);
- buffer = realloc(buffer, size);
+ buffer = xrealloc(buffer, size);
}
offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
buffer[offset++] = 0;