project_files/frontlib/util/refcounter.c
changeset 7497 7e1d72fc03c7
parent 7494 e65adfc99f15
child 7500 6253cae96f21
equal deleted inserted replaced
7494:e65adfc99f15 7497:7e1d72fc03c7
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU General Public License
       
     7  * as published by the Free Software Foundation; either version 2
       
     8  * of the License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    18  */
       
    19 
       
    20 #include "refcounter.h"
       
    21 
       
    22 #include "logging.h"
       
    23 
       
    24 void flib_retain(int *referenceCountPtr, const char *objName) {
       
    25 	if(!log_badargs_if2(referenceCountPtr==NULL, objName==NULL)) {
       
    26 		if((*referenceCountPtr)  >= 0) {
       
    27 			(*referenceCountPtr)++;
       
    28 			flib_log_d("retaining %s, now %i references", objName, (*referenceCountPtr));
       
    29 		}
       
    30 		if((*referenceCountPtr) < 0) {
       
    31 			flib_log_e("Memory leak: Reference count overflow in %s object!", objName);
       
    32 		}
       
    33 	}
       
    34 }
       
    35 
       
    36 /**
       
    37  * Returns true if the struct should be freed.
       
    38  */
       
    39 bool flib_release(int *referenceCountPtr, const char *objName) {
       
    40 	bool result = false;
       
    41 	if(!log_badargs_if2(referenceCountPtr==NULL, objName==NULL)) {
       
    42 		if((*referenceCountPtr) > 0) {
       
    43 			if(--(*referenceCountPtr) == 0) {
       
    44 				flib_log_d("releasing and destroying %s", objName);
       
    45 				result = true;
       
    46 			} else {
       
    47 				flib_log_d("releasing %s, now %i references", objName, *referenceCountPtr);
       
    48 			}
       
    49 		} else if((*referenceCountPtr) == 0) {
       
    50 			flib_log_e("Attempt to release a %s with zero references!", objName);
       
    51 		}
       
    52 	}
       
    53 	return result;
       
    54 }