Print this page
XXXX all calloc() implementations should check overflow


 190                 page->size = totpage;
 191                 block = page->block;
 192                 block->next = 0;
 193                 block->status = FREE;
 194                 block->size = totpage - HDR_PAGE;
 195                 block->page = page;
 196         }
 197 
 198         split(block, size);
 199 #if     DEBUG
 200         scribble((ulong_t *)&block->memstart, NEWMEM, block->size);
 201 #endif
 202         block->status = BUSY;
 203         return (&block->memstart);
 204 }
 205 
 206 void *
 207 calloc(size_t num, size_t size)
 208 {
 209         void *  mp;

 210 
 211         num *= size;
 212         if ((mp = malloc(num)) == NULL)






 213                 return (NULL);
 214         (void) memset(mp, 0, num);





 215         return (mp);
 216 }
 217 
 218 void *
 219 realloc(void *ptr, size_t size)
 220 {
 221         struct block    *block;
 222         size_t          osize;
 223         void *          newptr;
 224 
 225         if (ptr == NULL)
 226                 return (malloc(size));
 227 
 228         /* LINTED */
 229         block = (struct block *)((char *)ptr - HDR_BLOCK);
 230         size = S_DROUND(size);
 231         osize = block->size;
 232 
 233         /*
 234          * Join block with next one if it is free




 190                 page->size = totpage;
 191                 block = page->block;
 192                 block->next = 0;
 193                 block->status = FREE;
 194                 block->size = totpage - HDR_PAGE;
 195                 block->page = page;
 196         }
 197 
 198         split(block, size);
 199 #if     DEBUG
 200         scribble((ulong_t *)&block->memstart, NEWMEM, block->size);
 201 #endif
 202         block->status = BUSY;
 203         return (&block->memstart);
 204 }
 205 
 206 void *
 207 calloc(size_t num, size_t size)
 208 {
 209         void *  mp;
 210         size_t  total;
 211 
 212         if (num == 0 || size == 0) {
 213                 total = 0;
 214         } else {
 215                 total = num * size;
 216 
 217                 /* check for overflow */
 218                 if (total / num != size) {
 219                         errno = ENOMEM;
 220                         return (NULL);
 221                 }
 222         }
 223 
 224         if ((mp = malloc(total)) == NULL)
 225                 return (NULL);
 226         (void) memset(mp, 0, total);
 227         return (mp);
 228 }
 229 
 230 void *
 231 realloc(void *ptr, size_t size)
 232 {
 233         struct block    *block;
 234         size_t          osize;
 235         void *          newptr;
 236 
 237         if (ptr == NULL)
 238                 return (malloc(size));
 239 
 240         /* LINTED */
 241         block = (struct block *)((char *)ptr - HDR_BLOCK);
 242         size = S_DROUND(size);
 243         osize = block->size;
 244 
 245         /*
 246          * Join block with next one if it is free