IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 637


Ignore:
Timestamp:
May 10, 2004, 5:14:49 PM (22 years ago)
Author:
desonia
Message:

added code for various memory testpoints.

Location:
trunk/psLib/test/sysUtils
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/test/sysUtils/Makefile

    r629 r637  
    33##  Makefile:   test/sysUtils
    44##
    5 ##  $Revision: 1.8 $  $Name: not supported by cvs2svn $
    6 ##  $Date: 2004-05-10 18:22:22 $
     5##  $Revision: 1.9 $  $Name: not supported by cvs2svn $
     6##  $Date: 2004-05-11 03:14:49 $
    77##
    88##  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3939all: $(TARGET)
    4040
    41 atst_psAbort_01: atst_psAbort_01.o
    42 atst_psAbort_02: atst_psAbort_02.o
    43 atst_psAbort_03: atst_psAbort_03.o
    44 tst_psMemory: tst_psMemory.o
    45 tst_psError: tst_psError.o   
    46 tst_psHash00: tst_psHash00.o 
    47 tst_psHash01: tst_psHash01.o 
    48 tst_psHash02: tst_psHash02.o
    49 tst_psHash03: tst_psHash03.o
    50 tst_psHash04: tst_psHash04.o 
    51 tst_psLogMsg00: tst_psLogMsg00.o
    52 tst_psLogMsg01: tst_psLogMsg01.o
    53 tst_psLogMsg02: tst_psLogMsg02.o
    54 tst_psLogMsg03: tst_psLogMsg03.o
    55 tst_psStringCopy: tst_psStringCopy.o
    56 tst_psTrace00: tst_psTrace00.o
    57 tst_psTrace01: tst_psTrace01.o
    58 tst_psTrace02: tst_psTrace02.o
    59 tst_psTrace03: tst_psTrace03.o
    60 tst_psTrace04: tst_psTrace04.o
     41atst_psAbort_01:    atst_psAbort_01.o
     42atst_psAbort_02:    atst_psAbort_02.o
     43atst_psAbort_03:    atst_psAbort_03.o
     44tst_psMemory:       tst_psMemory.o
     45tst_psError:        tst_psError.o
     46tst_psHash00:       tst_psHash00.o
     47tst_psHash01:       tst_psHash01.o
     48tst_psHash02:       tst_psHash02.o
     49tst_psHash03:       tst_psHash03.o
     50tst_psHash04:       tst_psHash04.o
     51tst_psLogMsg00:     tst_psLogMsg00.o
     52tst_psLogMsg01:     tst_psLogMsg01.o
     53tst_psLogMsg02:     tst_psLogMsg02.o
     54tst_psLogMsg03:     tst_psLogMsg03.o
     55tst_psStringCopy:   tst_psStringCopy.o
     56tst_psTrace00:      tst_psTrace00.o
     57tst_psTrace01:      tst_psTrace01.o
     58tst_psTrace02:      tst_psTrace02.o
     59tst_psTrace03:      tst_psTrace03.o
     60tst_psTrace04:      tst_psTrace04.o
    6161
    6262clean:
     
    6565
    6666%.o : %.c
    67         $(CC) $(CFLAGS) $(CPPFLAGS) -I$(PSLIB_INCL_DIR) -c -o $@ $<
     67        $(CC) $(CFLAGS) $(CPPFLAGS) -I.. -I$(PSLIB_INCL_DIR) -c -o $@ $<
    6868
    6969%   : %.o
    70         $(CC) $(LDFLAGS) -L$(PSLIB_LIB_DIR) -lpslib -lpstest -o $@ $<
     70        $(CC) $(LDFLAGS) -L.. -L$(PSLIB_LIB_DIR) -lpslib -lpstest -o $@ $<
    7171
    7272%.lint: %.c
  • trunk/psLib/test/sysUtils/tst_psMemory.c

    r540 r637  
    1 /** @file  tstMemory.c
     1/** @file  tst_psMemory.c
    22 *
    33 *  @brief Contains the tests for psMemory.[ch]
     
    66 *  @author Robert DeSonia, MHPCC
    77 *
    8  *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2004-04-28 02:50:56 $
     8 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2004-05-11 03:14:49 $
    1010 *
    1111 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1212 */
     13
     14// need to allow malloc for callback use
     15#define PS_ALLOW_MALLOC 1
    1316
    1417#include <unistd.h>
     
    1619#include <sys/types.h>
    1720#include <sys/stat.h>
     21#include <limits.h>
     22#include <stdlib.h>
     23
    1824
    1925#include "psTest.h"
    20 #include "psMemory.h"
    21 #include "psAbort.h"
     26#include "psLib.h"
    2227
    2328static int TPFreeReferencedMemory(void);
     29static int TPOutOfMemory(void);
     30static void* TPOutOfMemoryExhaustedCallback(size_t size);
    2431static int TPCheckBufferPositive(void);
     32static int TPrealloc(void);
     33static int TPallocCallback(void);
     34static long memAllocateCallback(const psMemBlock *ptr);
     35static long memFreeCallback(const psMemBlock *ptr);
     36static int allocCallbackCalled = 0;
     37static int freeCallbackCalled = 0;
     38
     39bool TPOutOfMemoryExhaustedCallbackCalled = false;
    2540
    2641testDescription tests[] = {
    27                               {TPCheckBufferPositive,"TPCheckBufferPositive",0},
     42                              {TPCheckBufferPositive,"449-TPCheckBufferPositive",0},
     43                              {TPOutOfMemory,"450-TPOutOfMemory",0},
     44                              {TPrealloc,"451-TPrealloc",0},
     45                              {TPallocCallback,"452-TPallocCallback",0},
    2846                              {TPFreeReferencedMemory,"TPFreeReferencedMemory",6},
    2947                              {NULL}
     
    3250int main()
    3351{
    34     runTestSuite(stderr,"psMemory",tests);
    35 }
    36 
     52    psSetLogLevel(PS_LOG_INFO);
     53
     54    if (! runTestSuite(stderr,"psMemory",tests)) {
     55        psAbort(__FILE__,"One or more tests failed");
     56    }
     57    return 0;
     58}
     59
     60// Testpoint #449, psAlloc shall allocate memory blocks writeable by caller.
    3761int TPCheckBufferPositive(void)
    3862{
     
    4064    const int size = 100;
    4165    int failed = 0;
    42 
    43     printf("Allocating buffer\n");
     66    int currentId = psMemGetId();
     67
     68    psLogMsg(__func__,PS_LOG_INFO,"psAlloc shall allocate memory blocks writeable by caller.\n");
     69
    4470    mem = (int*) psAlloc(size*sizeof(int));
     71    if (mem == NULL) {
     72        psError(__FILE__,"psAlloc returned a NULL value in %s!",__func__);
     73        return 1;
     74    }
    4575
    4676    for (int index=0;index<size;index++) {
     
    5484    }
    5585
     86    psMemCheckLeaks(currentId,NULL,NULL);
     87    psMemCheckCorruption(1);
     88
    5689    return failed;
    5790}
     
    6194    // create memory
    6295    int* mem;
    63 
    64     printf("Allocating buffer.\n");
     96    int currentId = psMemGetId();
     97
     98    fprintf(stderr,"Allocating buffer.\n");
    6599    mem = (int*) psAlloc(100*sizeof(int));
    66100
    67101    psMemIncrRefCounter(mem);
    68     printf("Freeing buffer after incrementing reference count.\n");
     102    fprintf(stderr,"Freeing buffer after incrementing reference count.\n");
    69103    psFree(mem);
    70104
    71     return 0;
    72 }
    73 
     105    psMemCheckLeaks(currentId,NULL,NULL);
     106    psMemCheckCorruption(1);
     107
     108    return 0;
     109}
     110
     111// Testpoint #450,  Upon requesting more memory than is available, psalloc shall call
     112// the psMemExhaustedCallback.
     113int TPOutOfMemory(void)
     114{
     115    int currentId = psMemGetId();
     116    int* mem;
     117    psMemExhaustedCallback cb;
     118
     119    psLogMsg(__func__,PS_LOG_INFO,"Upon requesting more memory than is available, psalloc shall call "
     120             "the psMemExhaustedCallback.\n");
     121
     122    TPOutOfMemoryExhaustedCallbackCalled = false;
     123
     124    cb = psMemExhaustedCallbackSet(TPOutOfMemoryExhaustedCallback);
     125
     126    mem = (int*) psAlloc(INT_MAX);
     127
     128    psMemExhaustedCallbackSet(cb);
     129
     130    if (!TPOutOfMemoryExhaustedCallbackCalled) {
     131        psError(__FILE__,"Called psAlloc with HUGE memory requirement and survived in %s!",__func__);
     132        return 1;
     133    }
     134
     135    psFree(mem);
     136
     137    psMemCheckLeaks(currentId,NULL,NULL);
     138    psMemCheckCorruption(1);
     139
     140    return 0;
     141}
     142
     143
     144
     145void* TPOutOfMemoryExhaustedCallback(size_t size)
     146{
     147    psLogMsg(__func__,PS_LOG_INFO,"Custom MemExhaustedCallback was invoked.");
     148    TPOutOfMemoryExhaustedCallbackCalled = true;
     149    return malloc(1024);
     150}
     151
     152// Testpoint #451,  psRealloc shall increase/decrease memory buffer while preserving contents
     153int TPrealloc(void)
     154{
     155    int currentId = psMemGetId();
     156    int* mem1;
     157    int* mem2;
     158    int* mem3;
     159    const int initialSize = 100;
     160
     161    psLogMsg(__func__,PS_LOG_INFO,"psRealloc shall increase/decrease memory buffer while "
     162             "preserving contents");
     163
     164    // allocate buffer with known values.
     165    mem1 = (int*) psAlloc(initialSize*sizeof(int));
     166    mem2 = (int*) psAlloc(initialSize*sizeof(int));
     167    mem3 = (int*) psAlloc(initialSize*sizeof(int));
     168    for (int lcv=0;lcv<initialSize;lcv++) {
     169        mem1[lcv] = mem2[lcv] = mem3[lcv] = lcv;
     170    }
     171
     172    psMemCheckCorruption(1);
     173    psLogMsg(__func__,PS_LOG_INFO,"Expanding memory buffer.");
     174
     175    // realloc to 2x
     176    mem1 = (int*) psRealloc(mem1, 2*initialSize*sizeof(int));
     177    mem2 = (int*) psRealloc(mem2, 2*initialSize*sizeof(int));
     178    mem3 = (int*) psRealloc(mem3, 2*initialSize*sizeof(int));
     179
     180    // check values of initial block
     181    for (int i=0;i<initialSize;i++) {
     182        if (mem1[i] != i || mem2[i] != i || mem3[i] != i) {
     183            psError(__FILE__,"Realloc didn't preserve the contents with expanding buffer in %s.",
     184                    __func__);
     185            break;
     186        }
     187    }
     188
     189    psMemCheckCorruption(1);
     190    psLogMsg(__func__,PS_LOG_INFO,"Shrinking memory buffer.");
     191
     192    // realloc to 1/2 initial value.
     193    mem1 = (int*) psRealloc(mem1, (initialSize/2)*sizeof(int));
     194    mem2 = (int*) psRealloc(mem2, (initialSize/2)*sizeof(int));
     195    mem3 = (int*) psRealloc(mem3, (initialSize/2)*sizeof(int));
     196
     197    // check values of initial block
     198    for (int i=0;i<initialSize/2;i++) {
     199        if (mem1[i] != i || mem2[i] != i || mem3[i] != i) {
     200            psError(__FILE__,"Realloc didn't preserve the contents with shrinking buffer in %s.",
     201                    __func__);
     202            break;
     203        }
     204    }
     205
     206    psFree(mem1);
     207    psFree(mem2);
     208    psFree(mem3);
     209
     210    psMemCheckLeaks(currentId,NULL,NULL);
     211    psMemCheckCorruption(1);
     212
     213    return 0;
     214}
     215
     216int TPallocCallback(void)
     217{
     218    int* mem1;
     219    int* mem2;
     220    int* mem3;
     221    int currentId = psMemGetId();
     222    const int initialSize = 100;
     223    int mark;
     224
     225    allocCallbackCalled = 0;
     226    freeCallbackCalled = 0;
     227    psMemAllocateCallbackSet(memAllocateCallback);
     228    psMemFreeCallbackSet(memFreeCallback);
     229
     230    psMemAllocateCallbackSetID(currentId+1);
     231    psMemFreeCallbackSetID(currentId+1);
     232
     233    psLogMsg(__func__,PS_LOG_INFO,"call to psAlloc/psRealloc shall generate a callback if specified "
     234             "memory ID is allocated.");
     235
     236    // allocate buffer with known values.
     237    mem1 = (int*) psAlloc(initialSize*sizeof(int));
     238    mem2 = (int*) psAlloc(initialSize*sizeof(int));
     239    mem3 = (int*) psAlloc(initialSize*sizeof(int));
     240
     241    psFree(mem1);
     242    psFree(mem2);
     243    psFree(mem3);
     244
     245    if (allocCallbackCalled != 2 || freeCallbackCalled != 2) {
     246        psError(__FILE__,"alloc/free callbacks were not called the proper number of times in %s",
     247                __func__);
     248        return 1;
     249    }
     250
     251    allocCallbackCalled = 0;
     252    freeCallbackCalled = 0;
     253
     254    mark = psMemGetId();
     255
     256    mem1 = (int*) psAlloc(initialSize*sizeof(int));
     257
     258    psMemAllocateCallbackSetID(mark);
     259
     260    mem1 = (int*) psRealloc(mem1,initialSize*2*sizeof(int));
     261
     262    psFree(mem1);
     263
     264    if (allocCallbackCalled != 2) {
     265        psError(__FILE__,"realloc callbacks were not called the proper number of times in %s",
     266                __func__);
     267        return 1;
     268    }
     269    return 0;
     270
     271}
     272
     273long memAllocateCallback(const psMemBlock *ptr)
     274{
     275    psLogMsg(__func__,PS_LOG_INFO,"block %d was (re)allocated",ptr->id);
     276    allocCallbackCalled++;
     277    return 1;
     278}
     279
     280long memFreeCallback(const psMemBlock *ptr)
     281{
     282    psLogMsg(__func__,PS_LOG_INFO,"block %d was freed",ptr->id);
     283    freeCallbackCalled++;
     284    return 1;
     285}
Note: See TracChangeset for help on using the changeset viewer.