IMG_xpm.c
changeset 658 26061e601c81
parent 657 95fc7da55247
child 702 70e300b1be52
equal deleted inserted replaced
657:95fc7da55247 658:26061e601c81
    99     int bytes, s;
    99     int bytes, s;
   100     struct color_hash *hash;
   100     struct color_hash *hash;
   101 
   101 
   102     /* we know how many entries we need, so we can allocate
   102     /* we know how many entries we need, so we can allocate
   103        everything here */
   103        everything here */
   104     hash = (struct color_hash *)SDL_malloc(sizeof *hash);
   104     hash = (struct color_hash *)SDL_calloc(1, sizeof(*hash));
   105     if (!hash)
   105     if (!hash)
   106         return NULL;
   106         return NULL;
   107 
   107 
   108     /* use power-of-2 sized hash table for decoding speed */
   108     /* use power-of-2 sized hash table for decoding speed */
   109     for (s = STARTING_HASH_SIZE; s < maxnum; s <<= 1)
   109     for (s = STARTING_HASH_SIZE; s < maxnum; s <<= 1)
   110         ;
   110         ;
   111     hash->size = s;
   111     hash->size = s;
   112     hash->maxnum = maxnum;
   112     hash->maxnum = maxnum;
       
   113 
   113     bytes = hash->size * sizeof(struct hash_entry **);
   114     bytes = hash->size * sizeof(struct hash_entry **);
   114     hash->entries = NULL;   /* in case malloc fails */
   115     /* Check for overflow */
   115     hash->table = (struct hash_entry **)SDL_malloc(bytes);
   116     if ((bytes / sizeof(struct hash_entry **)) != hash->size) {
       
   117         IMG_SetError("memory allocation overflow");
       
   118         SDL_free(hash);
       
   119         return NULL;
       
   120     }
       
   121     hash->table = (struct hash_entry **)SDL_calloc(1, bytes);
   116     if (!hash->table) {
   122     if (!hash->table) {
   117         SDL_free(hash);
   123         SDL_free(hash);
   118         return NULL;
   124         return NULL;
   119     }
   125     }
   120     SDL_memset(hash->table, 0, bytes);
   126 
   121     hash->entries = (struct hash_entry *)SDL_malloc(maxnum * sizeof(struct hash_entry));
   127     bytes = maxnum * sizeof(struct hash_entry);
       
   128     /* Check for overflow */
       
   129     if ((bytes / sizeof(struct hash_entry)) != maxnum) {
       
   130         IMG_SetError("memory allocation overflow");
       
   131         SDL_free(hash->table);
       
   132         SDL_free(hash);
       
   133         return NULL;
       
   134     }
       
   135     hash->entries = (struct hash_entry *)SDL_calloc(1, bytes);
   122     if (!hash->entries) {
   136     if (!hash->entries) {
   123         SDL_free(hash->table);
   137         SDL_free(hash->table);
   124         SDL_free(hash);
   138         SDL_free(hash);
   125         return NULL;
   139         return NULL;
   126     }
   140     }