diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2015-03-17 17:49:28 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2015-03-17 17:49:28 +0000 |
commit | ce0e1bac0d71c79e631e6c041cf7e1820b3896bb (patch) | |
tree | 29b99043725b36007dc2157b5fcf552462d992ac | |
parent | 8198c038aec798ab3af5869cb13307102659a4c7 (diff) |
Add sort_calloc() and use it.
-rw-r--r-- | usr.bin/sort/coll.c | 8 | ||||
-rw-r--r-- | usr.bin/sort/file.c | 5 | ||||
-rw-r--r-- | usr.bin/sort/mem.c | 15 | ||||
-rw-r--r-- | usr.bin/sort/mem.h | 3 | ||||
-rw-r--r-- | usr.bin/sort/radixsort.c | 17 |
5 files changed, 26 insertions, 22 deletions
diff --git a/usr.bin/sort/coll.c b/usr.bin/sort/coll.c index a5badf5c9eb..047b15b4810 100644 --- a/usr.bin/sort/coll.c +++ b/usr.bin/sort/coll.c @@ -1,4 +1,4 @@ -/* $OpenBSD: coll.c,v 1.1 2015/03/17 17:45:13 millert Exp $ */ +/* $OpenBSD: coll.c,v 1.2 2015/03/17 17:49:27 millert Exp $ */ /*- * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> @@ -70,8 +70,7 @@ keys_array_alloc(void) size_t sz; sz = keys_array_size(); - ka = sort_malloc(sz); - memset(ka, 0, sz); + ka = sort_calloc(1, sz); return ka; } @@ -141,8 +140,7 @@ sort_list_item_alloc(void) size_t sz; sz = sizeof(struct sort_list_item) + keys_array_size(); - si = sort_malloc(sz); - memset(si, 0, sz); + si = sort_calloc(1, sz); return si; } diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c index 4b5ae8d5c8b..42d87714ffa 100644 --- a/usr.bin/sort/file.c +++ b/usr.bin/sort/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.1 2015/03/17 17:45:13 millert Exp $ */ +/* $OpenBSD: file.c,v 1.2 2015/03/17 17:49:27 millert Exp $ */ /*- * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> @@ -621,8 +621,7 @@ file_reader_init(const char *fsrc) if (fsrc == NULL) fsrc = "-"; - ret = sort_malloc(sizeof(struct file_reader)); - memset(ret, 0, sizeof(struct file_reader)); + ret = sort_calloc(1, sizeof(struct file_reader)); ret->elsymb = '\n'; if (sort_opts_vals.zflag) diff --git a/usr.bin/sort/mem.c b/usr.bin/sort/mem.c index 897af3d4371..c223292a1ff 100644 --- a/usr.bin/sort/mem.c +++ b/usr.bin/sort/mem.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mem.c,v 1.1 2015/03/17 17:45:13 millert Exp $ */ +/* $OpenBSD: mem.c,v 1.2 2015/03/17 17:49:27 millert Exp $ */ /*- * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> @@ -48,6 +48,19 @@ sort_malloc(size_t size) } /* + * calloc() wrapper. + */ +void * +sort_calloc(size_t nmemb, size_t size) +{ + void *ptr; + + if ((ptr = calloc(nmemb, size)) == NULL) + err(2, NULL); + return ptr; +} + +/* * free() wrapper. */ void diff --git a/usr.bin/sort/mem.h b/usr.bin/sort/mem.h index db172d5a12e..ae090781a79 100644 --- a/usr.bin/sort/mem.h +++ b/usr.bin/sort/mem.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mem.h,v 1.1 2015/03/17 17:45:13 millert Exp $ */ +/* $OpenBSD: mem.h,v 1.2 2015/03/17 17:49:27 millert Exp $ */ /*- * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> @@ -37,6 +37,7 @@ /* * mem.c */ +void *sort_calloc(size_t nmemb, size_t size); void *sort_malloc(size_t); void sort_free(void *ptr); void *sort_realloc(void *, size_t); diff --git a/usr.bin/sort/radixsort.c b/usr.bin/sort/radixsort.c index ad83aebf3df..59f65ace16c 100644 --- a/usr.bin/sort/radixsort.c +++ b/usr.bin/sort/radixsort.c @@ -1,4 +1,4 @@ -/* $OpenBSD: radixsort.c,v 1.1 2015/03/17 17:45:13 millert Exp $ */ +/* $OpenBSD: radixsort.c,v 1.2 2015/03/17 17:49:27 millert Exp $ */ /*- * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com> @@ -119,9 +119,7 @@ add_to_sublevel(struct sort_level *sl, struct sort_list_item *item, size_t indx) ssl = sl->sublevels[indx]; if (ssl == NULL) { - ssl = sort_malloc(sizeof(struct sort_level)); - memset(ssl, 0, sizeof(struct sort_level)); - + ssl = sort_calloc(1, sizeof(struct sort_level)); ssl->level = sl->level + 1; sl->sublevels[indx] = ssl; @@ -274,9 +272,7 @@ run_sort_level_next(struct sort_level *sl) } sl->sln = 256; - sl->sublevels = sort_malloc(slsz); - memset(sl->sublevels, 0, slsz); - + sl->sublevels = sort_calloc(1, slsz); sl->real_sln = 0; tosort_num = sl->tosort_num; @@ -391,8 +387,7 @@ run_top_sort_level(struct sort_level *sl) sl->start_position = 0; sl->sln = 256; - sl->sublevels = sort_malloc(slsz); - memset(sl->sublevels, 0, slsz); + sl->sublevels = sort_calloc(1, slsz); for (i = 0; i < sl->tosort_num; ++i) place_item(sl, i); @@ -459,9 +454,7 @@ rxsort(struct sort_list_item **base, size_t nmemb) { struct sort_level *sl; - sl = sort_malloc(sizeof(struct sort_level)); - memset(sl, 0, sizeof(struct sort_level)); - + sl = sort_calloc(1, sizeof(struct sort_level)); sl->tosort = base; sl->tosort_num = nmemb; sl->tosort_sz = nmemb; |