Print this page
4922 all calloc() implementations should check for overflow


  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*      Copyright (c) 1988 AT&T     */
  28 /*        All Rights Reserved   */
  29 
  30 #pragma ident   "%Z%%M% %I%     %E% SMI"
  31 
  32 #include <sys/types.h>
  33 
  34 #ifndef debug
  35 #define NDEBUG
  36 #endif
  37 
  38 #include <stdlib.h>
  39 #include <string.h>

  40 #include "assert.h"
  41 #include "malloc.h"
  42 #include "mallint.h"
  43 #include <thread.h>
  44 #include <pthread.h>
  45 #include <synch.h>
  46 #include <unistd.h>
  47 #include <limits.h>
  48 
  49 static mutex_t mlock = DEFAULTMUTEX;
  50 static ssize_t freespace(struct holdblk *);
  51 static void *malloc_unlocked(size_t, int);
  52 static void *realloc_unlocked(void *, size_t);
  53 static void free_unlocked(void *);
  54 static void *morecore(size_t);
  55 
  56 /*
  57  * use level memory allocater (malloc, free, realloc)
  58  *
  59  *      -malloc, free, realloc and mallopt form a memory allocator


 828                         cpysize = (size > cpysize) ? cpysize : size;
 829                         newptr = malloc_unlocked(size, 0);
 830                         if (newptr == NULL)
 831                                 return (NULL);
 832                         (void) memcpy(newptr, ptr, cpysize);
 833                         free_unlocked(ptr);
 834                 }
 835         }
 836         return (newptr);
 837 }
 838 
 839 
 840 /*
 841  * calloc - allocate and clear memory block
 842  */
 843 
 844 void *
 845 calloc(size_t num, size_t size)
 846 {
 847         char *mp;













 848 
 849         num *= size;
 850         mp = malloc(num);
 851         if (mp == NULL)
 852                 return (NULL);
 853         (void) memset(mp, 0, num);
 854         return (mp);
 855 }
 856 
 857 
 858 /*
 859  * Mallopt - set options for allocation
 860  *
 861  *      Mallopt provides for control over the allocation algorithm.
 862  *      The cmds available are:
 863  *
 864  *      M_MXFAST Set maxfast to value.  Maxfast is the size of the
 865  *               largest small, quickly allocated block.  Maxfast
 866  *               may be set to 0 to disable fast allocation entirely.
 867  *
 868  *      M_NLBLKS Set numlblks to value.  Numlblks is the number of
 869  *               small blocks per holding block.  Value must be
 870  *               greater than 0.
 871  *
 872  *      M_GRAIN  Set grain to value.  The sizes of all blocks
 873  *               smaller than maxfast are considered to be rounded




  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*      Copyright (c) 1988 AT&T     */
  28 /*        All Rights Reserved   */
  29 


  30 #include <sys/types.h>
  31 
  32 #ifndef debug
  33 #define NDEBUG
  34 #endif
  35 
  36 #include <stdlib.h>
  37 #include <string.h>
  38 #include <errno.h>
  39 #include "assert.h"
  40 #include "malloc.h"
  41 #include "mallint.h"
  42 #include <thread.h>
  43 #include <pthread.h>
  44 #include <synch.h>
  45 #include <unistd.h>
  46 #include <limits.h>
  47 
  48 static mutex_t mlock = DEFAULTMUTEX;
  49 static ssize_t freespace(struct holdblk *);
  50 static void *malloc_unlocked(size_t, int);
  51 static void *realloc_unlocked(void *, size_t);
  52 static void free_unlocked(void *);
  53 static void *morecore(size_t);
  54 
  55 /*
  56  * use level memory allocater (malloc, free, realloc)
  57  *
  58  *      -malloc, free, realloc and mallopt form a memory allocator


 827                         cpysize = (size > cpysize) ? cpysize : size;
 828                         newptr = malloc_unlocked(size, 0);
 829                         if (newptr == NULL)
 830                                 return (NULL);
 831                         (void) memcpy(newptr, ptr, cpysize);
 832                         free_unlocked(ptr);
 833                 }
 834         }
 835         return (newptr);
 836 }
 837 
 838 
 839 /*
 840  * calloc - allocate and clear memory block
 841  */
 842 
 843 void *
 844 calloc(size_t num, size_t size)
 845 {
 846         char *mp;
 847         size_t total;
 848 
 849         if (num == 0 || size == 0) {
 850                 total = 0;
 851         } else {
 852                 total = num * size;
 853 
 854                 /* check for overflow */
 855                 if ((total / num) != size) {
 856                         errno = ENOMEM;
 857                         return (NULL);
 858                 }
 859         }
 860 
 861         mp = malloc(total);

 862         if (mp == NULL)
 863                 return (NULL);
 864         (void) memset(mp, 0, total);
 865         return (mp);
 866 }
 867 
 868 
 869 /*
 870  * Mallopt - set options for allocation
 871  *
 872  *      Mallopt provides for control over the allocation algorithm.
 873  *      The cmds available are:
 874  *
 875  *      M_MXFAST Set maxfast to value.  Maxfast is the size of the
 876  *               largest small, quickly allocated block.  Maxfast
 877  *               may be set to 0 to disable fast allocation entirely.
 878  *
 879  *      M_NLBLKS Set numlblks to value.  Numlblks is the number of
 880  *               small blocks per holding block.  Value must be
 881  *               greater than 0.
 882  *
 883  *      M_GRAIN  Set grain to value.  The sizes of all blocks
 884  *               smaller than maxfast are considered to be rounded