"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.8.3/src/contrib/mempattern.c" (16 Jul 2019, 2470 Bytes) of package /linux/misc/dns/knot-2.8.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mempattern.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.7.6_vs_2.8.0.

    1 /*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
    2 
    3     This program is free software: you can redistribute it and/or modify
    4     it under the terms of the GNU General Public License as published by
    5     the Free Software Foundation, either version 3 of the License, or
    6     (at your option) any later version.
    7 
    8     This program is distributed in the hope that it will be useful,
    9     but WITHOUT ANY WARRANTY; without even the implied warranty of
   10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11     GNU General Public License for more details.
   12 
   13     You should have received a copy of the GNU General Public License
   14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
   15  */
   16 
   17 #include <stdlib.h>
   18 
   19 #include "contrib/mempattern.h"
   20 #include "contrib/string.h"
   21 #include "contrib/ucw/mempool.h"
   22 
   23 static void mm_nofree(void *p)
   24 {
   25     /* nop */
   26 }
   27 
   28 static void *mm_malloc(void *ctx, size_t n)
   29 {
   30     (void)ctx;
   31     return malloc(n);
   32 }
   33 
   34 void *mm_alloc(knot_mm_t *mm, size_t size)
   35 {
   36     if (mm) {
   37         return mm->alloc(mm->ctx, size);
   38     } else {
   39         return malloc(size);
   40     }
   41 }
   42 
   43 void *mm_calloc(knot_mm_t *mm, size_t nmemb, size_t size)
   44 {
   45     if (nmemb == 0 || size == 0) {
   46         return NULL;
   47     }
   48     if (mm) {
   49         size_t total_size = nmemb * size;
   50         if (total_size / nmemb != size) { // Overflow check
   51             return NULL;
   52         }
   53         void *mem = mm_alloc(mm, total_size);
   54         if (mem == NULL) {
   55             return NULL;
   56         }
   57         return memzero(mem, total_size);
   58     } else {
   59         return calloc(nmemb, size);
   60     }
   61 }
   62 
   63 void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
   64 {
   65     if (mm) {
   66         void *p = mm->alloc(mm->ctx, size);
   67         if (p == NULL) {
   68             return NULL;
   69         } else {
   70             if (what) {
   71                 memcpy(p, what,
   72                        prev_size < size ? prev_size : size);
   73             }
   74             mm_free(mm, what);
   75             return p;
   76         }
   77     } else {
   78         return realloc(what, size);
   79     }
   80 }
   81 
   82 char *mm_strdup(knot_mm_t *mm, const char *s)
   83 {
   84     if (s == NULL) {
   85         return NULL;
   86     }
   87     if (mm) {
   88         size_t len = strlen(s) + 1;
   89         void *mem = mm_alloc(mm, len);
   90         if (mem == NULL) {
   91             return NULL;
   92         }
   93         return memcpy(mem, s, len);
   94     } else {
   95         return strdup(s);
   96     }
   97 }
   98 
   99 void mm_free(knot_mm_t *mm, void *what)
  100 {
  101     if (mm) {
  102         if (mm->free) {
  103             mm->free(what);
  104         }
  105     } else {
  106         free(what);
  107     }
  108 }
  109 
  110 void mm_ctx_init(knot_mm_t *mm)
  111 {
  112     mm->ctx = NULL;
  113     mm->alloc = mm_malloc;
  114     mm->free = free;
  115 }
  116 
  117 void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size)
  118 {
  119     mm->ctx = mp_new(chunk_size);
  120     mm->alloc = (knot_mm_alloc_t)mp_alloc;
  121     mm->free = mm_nofree;
  122 }