Main Page Modules Data Structures File List Data Fields Globals Related Pages

# beecrypt/memchunk.c

Go to the documentation of this file.

    00001 /*
    00002  * memchunk.c
    00003  *
    00004  * BeeCrypt memory block handling, code
    00005  *
    00006  * Copyright (c) 2001 Virtual Unlimited B.V.
    00007  *
    00008  * Author: Bob Deblier <bob@virtualunlimited.com>
    00009  *
    00010  * This library is free software; you can redistribute it and/or
    00011  * modify it under the terms of the GNU Lesser General Public
    00012  * License as published by the Free Software Foundation; either
    00013  * version 2.1 of the License, or (at your option) any later version.
    00014  *
    00015  * This library is distributed in the hope that it will be useful,
    00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00018  * Lesser General Public License for more details.
    00019  *
    00020  * You should have received a copy of the GNU Lesser General Public
    00021  * License along with this library; if not, write to the Free Software
    00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00023  *
    00024  */
    00025
    00026 #include "system.h"
    00027 #include "memchunk.h"
    00028 #include "debug.h"
    00029
    00030 /*@-compdef@*/  /* tmp->data is undefined */
    00031 memchunk* memchunkAlloc(int size)
    00032 {
    00033         memchunk* tmp = (memchunk*) calloc(1, sizeof(*tmp));
    00034
    00035         if (tmp)
    00036         {
    00037                 tmp->size = size;
    00038                 /*@-mustfree@*/ /* tmp->data is OK */
    00039                 tmp->data = (byte*) malloc(size);
    00040                 /*@=mustfree@*/
    00041
    00042                 if (tmp->data == (byte*) 0)
    00043                 {
    00044                         free(tmp);
    00045                         tmp = 0;
    00046                 }
    00047         }
    00048
    00049         return tmp;
    00050 }
    00051 /*@=compdef@*/
    00052
    00053 void memchunkFree(memchunk* m)
    00054 {
    00055         if (m)
    00056         {
    00057                 if (m->data)
    00058                 {
    00059                         free(m->data);
    00060
    00061                         m->size = 0;
    00062                         m->data = (byte*) 0;
    00063                 }
    00064                 free(m);
    00065         }
    00066 }
    00067
    00068 memchunk* memchunkResize(memchunk* m, int size)
    00069 {
    00070         /*@-branchstate@*/
    00071         if (m)
    00072         {
    00073                 if (m->data)
    00074                         m->data = (byte*) realloc(m->data, size);
    00075                 else
    00076                         m->data = (byte*) malloc(size);
    00077
    00078                 if (m->data == (byte*) 0)
    00079                 {
    00080                         free(m);
    00081                         m = (memchunk*) 0;
    00082                 }
    00083                 else
    00084                         /*@-nullderef@*/
    00085                         m->size = size;
    00086                         /*@=nullderef@*/
    00087         }
    00088         /*@=branchstate@*/
    00089
    00090         /*@-nullret -compdef @*/        /* LCL: m->data might be NULL */
    00091         return m;
    00092         /*@=nullret =compdef@*/
    00093 }
    00094
    00095 /*@-boundswrite@*/
    00096 memchunk* memchunkClone(const memchunk* m)
    00097 {
    00098         if (m)
    00099         {
    00100                 memchunk* tmp = memchunkAlloc(m->size);
    00101
    00102                 if (tmp)
    00103                         memcpy(tmp->data, m->data, m->size);
    00104
    00105                 return tmp;
    00106         }
    00107
    00108         return (memchunk*) 0;
    00109 }
    00110 /*@=boundswrite@*/