[sheepdog] [PATCH 3/6] ulil: add self-panic xvalloc() helper
Liu Yuan
namei.unix at gmail.com
Sun Jan 27 08:43:44 CET 2013
From: Liu Yuan <tailai.ly at taobao.com>
Use this helper for callers of valloc() which can't stand its failure.
Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
include/util.h | 1 +
lib/util.c | 14 +++++++++++---
sheep/journal_file.c | 4 +---
sheep/object_cache.c | 10 ++--------
sheep/ops.c | 6 +-----
5 files changed, 16 insertions(+), 19 deletions(-)
diff --git a/include/util.h b/include/util.h
index eb1b9a6..109af06 100644
--- a/include/util.h
+++ b/include/util.h
@@ -76,6 +76,7 @@ void *xmalloc(size_t size);
void *xzalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
void *xcalloc(size_t nmemb, size_t size);
+void *xvalloc(size_t size);
ssize_t xread(int fd, void *buf, size_t len);
ssize_t xwrite(int fd, const void *buf, size_t len);
ssize_t xpread(int fd, void *buf, size_t count, off_t offset);
diff --git a/lib/util.c b/lib/util.c
index 86dcfd6..79249bc 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -49,7 +49,7 @@ void *xmalloc(size_t size)
if (!ret && !size)
ret = malloc(1);
if (!ret)
- panic("Out of memory");
+ panic("Out of memory\n");
}
return ret;
}
@@ -70,7 +70,7 @@ notrace void *xrealloc(void *ptr, size_t size)
if (!ret && !size)
ret = realloc(ptr, 1);
if (!ret)
- panic("Out of memory");
+ panic("Out of memory\n");
}
return ret;
}
@@ -86,11 +86,19 @@ void *xcalloc(size_t nmemb, size_t size)
if (!ret && (!nmemb || !size))
ret = calloc(1, 1);
if (!ret)
- panic("Out of memory");
+ panic("Out of memory\n");
}
return ret;
}
+void *xvalloc(size_t size)
+{
+ void *ret = valloc(size);
+ if (!ret)
+ panic("Out of memory\n");
+ return ret;
+}
+
static ssize_t _read(int fd, void *buf, size_t len)
{
ssize_t nr;
diff --git a/sheep/journal_file.c b/sheep/journal_file.c
index 799f01f..c5d31e8 100644
--- a/sheep/journal_file.c
+++ b/sheep/journal_file.c
@@ -338,9 +338,7 @@ int journal_file_write(uint64_t oid, const char *buf, size_t size,
jfile.pos += wsize;
pthread_spin_unlock(&jfile_lock);
- p = wbuffer = valloc(wsize);
- if (!wbuffer)
- panic("%m\n");
+ p = wbuffer = xvalloc(wsize);
memcpy(p, &jd, JOURNAL_DESC_SIZE);
p += JOURNAL_DESC_SIZE;
memcpy(p, buf, size);
diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index caed7a3..3cccca3 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -395,10 +395,7 @@ static int push_cache_object(uint32_t vid, uint32_t idx, uint64_t bmap,
if (is_vdi_obj(oid) && (offset + data_length) > SD_INODE_SIZE)
data_length = SD_INODE_SIZE - offset;
- buf = valloc(data_length);
- if (buf == NULL)
- panic("failed to allocate memory\n");
-
+ buf = xvalloc(data_length);
ret = read_cache_object_noupdate(vid, idx, buf, data_length, offset);
if (ret != SD_RES_SUCCESS)
goto out;
@@ -792,10 +789,7 @@ static int object_cache_pull(struct object_cache *oc, uint32_t idx)
uint32_t data_length = get_objsize(oid);
void *buf;
- buf = valloc(data_length);
- if (!buf)
- panic("%m\n");
-
+ buf = xvalloc(data_length);
sd_init_req(&hdr, SD_OP_READ_OBJ);
hdr.data_length = data_length;
hdr.obj.oid = oid;
diff --git a/sheep/ops.c b/sheep/ops.c
index bc06acf..b660b9f 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -853,11 +853,7 @@ int peer_create_and_write_obj(struct request *req)
if (hdr->flags & SD_FLAG_CMD_COW) {
sd_dprintf("%" PRIx64 ", %" PRIx64 "\n", oid, hdr->obj.cow_oid);
- buf = valloc(SD_DATA_OBJ_SIZE);
- if (!buf) {
- sd_eprintf("can not allocate memory\n");
- goto out;
- }
+ buf = xvalloc(SD_DATA_OBJ_SIZE);
if (hdr->data_length != SD_DATA_OBJ_SIZE) {
ret = read_copy_from_replica(req, hdr->epoch,
hdr->obj.cow_oid, buf);
--
1.7.9.5
More information about the sheepdog
mailing list