| 1 | /*BHEADER**********************************************************************
|
|---|
| 2 | * Copyright (c) 2008, Lawrence Livermore National Security, LLC.
|
|---|
| 3 | * Produced at the Lawrence Livermore National Laboratory.
|
|---|
| 4 | * This file is part of HYPRE. See file COPYRIGHT for details.
|
|---|
| 5 | *
|
|---|
| 6 | * HYPRE is free software; you can redistribute it and/or modify it under the
|
|---|
| 7 | * terms of the GNU Lesser General Public License (as published by the Free
|
|---|
| 8 | * Software Foundation) version 2.1 dated February 1999.
|
|---|
| 9 | *
|
|---|
| 10 | * $Revision: 2.4 $
|
|---|
| 11 | ***********************************************************************EHEADER*/
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | /******************************************************************************
|
|---|
| 15 | *
|
|---|
| 16 | * Box allocation routines. These hopefully increase efficiency
|
|---|
| 17 | * and reduce memory fragmentation.
|
|---|
| 18 | *
|
|---|
| 19 | *****************************************************************************/
|
|---|
| 20 |
|
|---|
| 21 | #include "headers.h"
|
|---|
| 22 |
|
|---|
| 23 | /*--------------------------------------------------------------------------
|
|---|
| 24 | * Box memory data structure and static variables used to manage free
|
|---|
| 25 | * list and blocks to be freed by the finalization routine.
|
|---|
| 26 | *--------------------------------------------------------------------------*/
|
|---|
| 27 |
|
|---|
| 28 | union box_memory
|
|---|
| 29 | {
|
|---|
| 30 | union box_memory *d_next;
|
|---|
| 31 | hypre_Box d_box;
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | static union box_memory *s_free = NULL;
|
|---|
| 35 | static union box_memory *s_finalize = NULL;
|
|---|
| 36 | static int s_at_a_time = 1000;
|
|---|
| 37 | static int s_count = 0;
|
|---|
| 38 |
|
|---|
| 39 | /*--------------------------------------------------------------------------
|
|---|
| 40 | * Allocate a new block of memory and thread it into the free list. The
|
|---|
| 41 | * first block will always be put on the finalize list to be freed by
|
|---|
| 42 | * the hypre_BoxFinalizeMemory() routine to remove memory leaks.
|
|---|
| 43 | *--------------------------------------------------------------------------*/
|
|---|
| 44 |
|
|---|
| 45 | static int
|
|---|
| 46 | hypre_AllocateBoxBlock()
|
|---|
| 47 | {
|
|---|
| 48 | int ierr = 0;
|
|---|
| 49 | union box_memory *ptr;
|
|---|
| 50 | int i;
|
|---|
| 51 |
|
|---|
| 52 | ptr = hypre_TAlloc(union box_memory, s_at_a_time);
|
|---|
| 53 | ptr[0].d_next = s_finalize;
|
|---|
| 54 | s_finalize = &ptr[0];
|
|---|
| 55 |
|
|---|
| 56 | for (i = (s_at_a_time - 1); i > 0; i--)
|
|---|
| 57 | {
|
|---|
| 58 | ptr[i].d_next = s_free;
|
|---|
| 59 | s_free = &ptr[i];
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | return ierr;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /*--------------------------------------------------------------------------
|
|---|
| 66 | * Set up the allocation block size and allocate the first memory block.
|
|---|
| 67 | *--------------------------------------------------------------------------*/
|
|---|
| 68 |
|
|---|
| 69 | int
|
|---|
| 70 | hypre_BoxInitializeMemory( const int at_a_time )
|
|---|
| 71 | {
|
|---|
| 72 | int ierr = 0;
|
|---|
| 73 |
|
|---|
| 74 | if (at_a_time > 0)
|
|---|
| 75 | {
|
|---|
| 76 | s_at_a_time = at_a_time;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | return ierr;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /*--------------------------------------------------------------------------
|
|---|
| 83 | * Free all of the memory used to manage boxes. This should only be
|
|---|
| 84 | * called at the end of the program to collect free memory. The blocks
|
|---|
| 85 | * in the finalize list are freed.
|
|---|
| 86 | *--------------------------------------------------------------------------*/
|
|---|
| 87 |
|
|---|
| 88 | int
|
|---|
| 89 | hypre_BoxFinalizeMemory()
|
|---|
| 90 | {
|
|---|
| 91 | int ierr = 0;
|
|---|
| 92 | union box_memory *byebye;
|
|---|
| 93 |
|
|---|
| 94 | while (s_finalize)
|
|---|
| 95 | {
|
|---|
| 96 | byebye = s_finalize;
|
|---|
| 97 | s_finalize = (s_finalize -> d_next);
|
|---|
| 98 | hypre_TFree(byebye);
|
|---|
| 99 | }
|
|---|
| 100 | s_finalize = NULL;
|
|---|
| 101 | s_free = NULL;
|
|---|
| 102 |
|
|---|
| 103 | return ierr;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /*--------------------------------------------------------------------------
|
|---|
| 107 | * Allocate a box from the free list. If no boxes exist on the free
|
|---|
| 108 | * list, then allocate a block of memory to repopulate the free list.
|
|---|
| 109 | *--------------------------------------------------------------------------*/
|
|---|
| 110 |
|
|---|
| 111 | hypre_Box *
|
|---|
| 112 | hypre_BoxAlloc()
|
|---|
| 113 | {
|
|---|
| 114 | union box_memory *ptr = NULL;
|
|---|
| 115 |
|
|---|
| 116 | if (!s_free)
|
|---|
| 117 | {
|
|---|
| 118 | hypre_AllocateBoxBlock();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | ptr = s_free;
|
|---|
| 122 | s_free = (s_free -> d_next);
|
|---|
| 123 | s_count++;
|
|---|
| 124 | return( &(ptr -> d_box) );
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /*--------------------------------------------------------------------------
|
|---|
| 128 | * Put a box back on the free list.
|
|---|
| 129 | *--------------------------------------------------------------------------*/
|
|---|
| 130 |
|
|---|
| 131 | int
|
|---|
| 132 | hypre_BoxFree( hypre_Box *box )
|
|---|
| 133 | {
|
|---|
| 134 | int ierr = 0;
|
|---|
| 135 | union box_memory *ptr = (union box_memory *) box;
|
|---|
| 136 |
|
|---|
| 137 | (ptr -> d_next) = s_free;
|
|---|
| 138 | s_free = ptr;
|
|---|
| 139 | s_count--;
|
|---|
| 140 |
|
|---|
| 141 | if (!s_count)
|
|---|
| 142 | {
|
|---|
| 143 | hypre_BoxFinalizeMemory();
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | return ierr;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|