Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/test/malloc/malloc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. alloc
  2. test_1
  3. test_2
  4. test_3
  5. main

   1 /*
   2  * Copyright (c) 2005, Kohsuke Ohtani
   3  * All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the author nor the names of any co-contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * malloc.c - malloc test program.
  32  */
  33 
  34 #include <stdlib.h>
  35 #include <stdio.h>
  36 
  37 #define NR_ALLOCS       30
  38 
  39 static void *ptr[NR_ALLOCS];
  40 
  41 static char *
  42 alloc(int buflen)
  43 {
  44         char *p, *q;
  45         int i;
  46 
  47         printf("Allocate %d bytes - ", buflen);
  48 
  49         p = malloc((size_t)buflen);
  50         if (p == NULL) {
  51                 printf("Error: malloc() returns NULL!\n");
  52                 return NULL;
  53         }
  54         for (q = p, i = 0; i < buflen; i++)
  55                 *(q++) = '@';
  56 #if 0
  57         for (q = p, i = 0; i < buflen; i++)
  58                 putchar(*(q++));
  59 #endif
  60         printf("OK!\n");
  61         return p;
  62 }
  63 
  64 static void
  65 test_1(void)
  66 {
  67         char *p;
  68 
  69         printf("test_1 - start\n");
  70 
  71         p = alloc(1);
  72         free(p);
  73         p = alloc(2);
  74         free(p);
  75         p = alloc(256);
  76         free(p);
  77         p = alloc(1024);
  78         free(p);
  79         p = alloc(8096);
  80         free(p);
  81         p = alloc(-1);
  82         free(p);
  83 
  84         printf("test_1 - done\n");
  85 }
  86 
  87 static void
  88 test_2(void)
  89 {
  90         int i, j;
  91 
  92         printf("test_2 - start\n");
  93 
  94         for (i = 0; i < NR_ALLOCS; i++)
  95                 ptr[i] = alloc(random() & 0xf);
  96         for (i = 0; i < NR_ALLOCS; i++)
  97                 free(ptr[i]);
  98 
  99         for (i = 0; i < NR_ALLOCS; i++)
 100                 ptr[i] = alloc(random() & 0xff);
 101         for (i = 0; i < NR_ALLOCS; i++)
 102                 free(ptr[i]);
 103 
 104         for (i = 0; i < NR_ALLOCS; i++)
 105                 ptr[i] = alloc(random() & 0xfff);
 106         for (i = 0; i < NR_ALLOCS; i++)
 107                 free(ptr[i]);
 108 
 109         for (i = 0; i < NR_ALLOCS; i++)
 110                 ptr[i] = alloc(random() & 0xfff);
 111         for (i = 0; i < 10000; i++) {
 112                 j = random() % NR_ALLOCS;
 113                 if (ptr[j] != NULL) {
 114                         free(ptr[j]);
 115                         ptr[j] = NULL;
 116                 }
 117         }
 118         printf("test_2 - done\n");
 119 }
 120 
 121 static void
 122 test_3(void)
 123 {
 124         char *p;
 125 
 126         printf("test_3 - start\n");
 127 
 128         p = alloc(256);
 129         free(p);
 130 
 131         printf("test_3 - try to free invalid area...\n");
 132         free(p); /* invalid */
 133 
 134         printf("test_3 - done!?\n");
 135 }
 136 
 137 int
 138 main(int argc, char *argv[])
 139 {
 140         printf("Malloc test program.\n");
 141 
 142         test_1();
 143         test_2();
 144         test_3();
 145 
 146         return 0;
 147 }

/* [<][>][^][v][top][bottom][index][help] */