On Saturday, February 28, 2015, Gabriela Gibson <[email protected]>
wrote:
> Hi All,
>
> Because I'm not quite sure what is all needed and to make this easier to
> discuss, I put the entire lot into a standalone proggy.
Do not be afraid of putting things in their rightful place, it makes life
easier for all of us, and you have to play with dftest soon.
When I make a new function, I often make a couple of test cases first,
simply to play with the parameters and see if it feels right.
> Let me know if anything is missing or should be different, I commented the
> code where I was not sure about certain things.
comments below, but the important one is above use JFDI, and then make (if
needed) small corrections afterward. If you were a committer (which you do
a good job of being) you would have to do it like that.
>
> My output is listed below the code.
>
> thanks,
>
> G
>
> Ps.: the string ": Cannot allocate memory" just pops up, I don't know where
> from, maybe stderr?
>
> yup atderr, from malloc internally.
> [[
> #define _GNU_SOURCE /* See feature_test_macros(7) */
> #include <sys/types.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <stdarg.h>
> #include <time.h>
> #include <unistd.h>
>
> void *xmalloc(size_t size)
> {
> void *ptr = malloc(size);
>
> if (ptr == NULL) {
> perror("xmalloc: out of memory.\n");
> /* _exit(EXIT_FAILURE); // commented out for testing */
> return NULL;
> }
>
> return ptr;
> }
>
> ok
> void xfree(void *ptr)
> {
> if (ptr == NULL)
> return;
> free(ptr);
> ptr = NULL;
> }
>
> remember your scope rules, ptr is a local variable so changes to that are
lost.
ok
> void *xcalloc(size_t nmemb, size_t size)
> {
> void *ptr = calloc(nmemb, size);
>
> if (ptr == NULL) {
> perror("xcalloc: out of memory.\n");
> /* _exit(EXIT_FAILURE); // commented out for testing */
> return NULL;
> }
>
> return ptr;
> }
>
> ok
> void *xrealloc(void *ptr, size_t size)
> {
> void *ret_ptr = realloc(ptr, size);
>
> if (ret_ptr == NULL) {
> perror("xrealloc: out of memory.\n");
> /* _exit(EXIT_FAILURE); // commented out for testing */
> return NULL;
> }
>
> return ret_ptr;
> }
>
> ok, please use the same variable name as in other functions. New name
makes me wonder is this soecial.
> int main (int argc, char *argv[])
> {
> printf("==================================================\n\n");
> printf("xmalloc tests:\n");
> {
> // what happens if we overallocate, check the NULL trap works
> int count = 0;
> while (1) {
while (10)
> void *p = xmalloc(1024*1024*1024);
> if (p == NULL) {
> printf("count = %d\n",count);
> printf("OK: overallocating xmalloc returned NULL\n\n");
see test/wrapper we have a macro to use when failing.
> break;
> }
> else {
> count++;
> }
> }
> }
ok test case 1
> { // what happens if size = 0
> size_t size = 0;
> void *p = xmalloc(size);
> if (p == NULL) {
> printf("Error: size = 0 -- xmalloc returned NULL ");
> }
> else
> printf("OK: size = 0 returns %p, %zu\n", p, sizeof(p));
> }
ok test case 2
> printf("==================================================\n\n");
> printf("xcalloc tests:\n");
> {
> // what happens if we overallocate, check the NULL trap works
> int count = 0;
> while (1) {
> void *p = xcalloc(1024*1024*1024, sizeof (int));
> if (p == NULL) {
> printf("count = %d\n",count);
> printf("OK: overallocating xcalloc returned NULL\n\n");
> break;
> }
> else {
> count++;
> }
> }
> }
does not work on all platforms, so let us only do positive tests.
> { // what happens if size = 0
>
> // Manual says: If nmemb or size is 0, then calloc() returns
> // either NULL, or a unique pointer value that can later be
> // successfully passed to free(). Since we set xfree to
> // protect against freeing NULL, we don't have to worry. (I
> // think) But, the NULL ptr may be used to assign other stuff
> // too, which is not so good.
>
> size_t size = 0;
> void *p = xcalloc(1024, size);
> if (p == NULL) {
> printf("Error: size = 0: xcalloc returned NULL\n");
> }
> else
> printf("OK: size = 0 returns %p, %zu\n", p, sizeof(p));
> }
>
> ok case 3
> // realloc test
> printf("==================================================\n\n");
> printf("xrealloc tests:\n");
> {
> void *p = xmalloc(1000);
> // what happens if we overallocate, check the NULL trap works
> int count = 0;
> while (1) {
> p = xrealloc(p, 1024*1024*1024);
> if (p == NULL) {
> printf("count = %d\n",count);
> printf("OK: overallocating realloc returned NULL\n\n");
> break;
> }
> else {
> count++;
> }
> }
> }
ok same comments as for xmalloc
> { // what happens if size = 0
> size_t size = 0;
> void *p = xmalloc(1000);
> p = xrealloc(p, size);
> if (p == NULL) {
> // should we force the returning of a pointer here so it
> // can be freed later?
> printf("Error: size = 0 test: xrealloc returned NULL\n");
> }
> else
> printf("OK: size = 0 returns pointer %p.\n", p);
> }
>
> ok
> { // what happens if we resize to a smaller allocation
> size_t size = 500;
> void *p = xmalloc(1000);
> p = xrealloc(p, size);
> if (p == NULL) {
> printf("Error: size = 0 test: xrealloc returned NULL.\n");
> }
> else {
> // actually, I'm not sure. I cannot think of a way to
> // measure the size allocated to a pointer. sizeof(p)
> // clearly is wrong here, since it only tells me how
> // large the pointer is, not how much it points to.
you cannot.
> printf("Result of resize: size required %zu -- "
> "returns pointer %p of size %zu.\n",
> size, p, sizeof(p));
> }
> }
remove else otherwise ok.
>
> printf("==================================================\n\n");
> printf("xfree tests:\n");
> {
> void *p = xmalloc(133);
> xfree(p);
> p = NULL;
> xfree(p);
> printf("xfree accepts NULL pointer.\n");
> xfree(p);
> printf("xfree accepts second attempt to free pointer.\n");
> }
> printf("==================================================\n\n");
> printf("Test complete.\n");
> return 1;
> }
ok but I suggest to add xfree in all the other test cases.
please think of positive test cases as examples of how to use the function.
good work, let us have the patch.
rgds
jan i
>
> ]]
>
> Output:
>
> ==================================================
>
> xmalloc tests:
> xmalloc: out of memory.
> : Cannot allocate memory
> count = 2
> OK: overallocating xmalloc returned NULL
>
> OK: size = 0 returns 0x37400468, 4
> ==================================================
>
> xcalloc tests:
> xcalloc: out of memory.
> : Cannot allocate memory
> count = 0
> OK: overallocating xcalloc returned NULL
>
> OK: size = 0 returns 0x37400478, 4
> ==================================================
>
> xrealloc tests:
> xrealloc: out of memory.
> : Cannot allocate memory
> count = 0
> OK: overallocating realloc returned NULL
>
> xrealloc: out of memory.
> : Cannot allocate memory
> Error: size = 0 test: xrealloc returned NULL
> Result of resize: size required 500 -- returns pointer 0x37400878 of size
> 4.
> ==================================================
>
> xfree tests:
> xfree accepts NULL pointer.
> xfree accepts second attempt to free pointer.
> ==================================================
>
> Test complete.
>
>
>
>
>
>
> --
> Visit my Coding Diary: http://gabriela-gibson.blogspot.com/
>
--
Sent from My iPad, sorry for any misspellings.