00001 /***************************************************************************** 00002 * Copyright 2005 Daniel Ferullo * 00003 * * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); * 00005 * you may not use this file except in compliance with the License. * 00006 * You may obtain a copy of the License at * 00007 * * 00008 * http://www.apache.org/licenses/LICENSE-2.0 * 00009 * * 00010 * Unless required by applicable law or agreed to in writing, software * 00011 * distributed under the License is distributed on an "AS IS" BASIS, * 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 00013 * See the License for the specific language governing permissions and * 00014 * limitations under the License. * 00015 * * 00016 *****************************************************************************/ 00017 00018 /** 00019 * @file util.c 00020 * @author Daniel Ferullo (ferullo@cmu.edu) 00021 * 00022 * @brief contains useful utility functions 00023 */ 00024 00025 #include "util.h" 00026 #include <stdlib.h> 00027 #include <unistd.h> 00028 #include "def.h" 00029 00030 /* inline function */ 00031 int inline DEC_UNTIL_ZERO(int x) { 00032 return (x>=1 ? x-1 : 0); 00033 } 00034 00035 errorcode safe_free(void *memory) { 00036 00037 /* declare local variables */ 00038 00039 /* error check arguments */ 00040 CHECK_NOT_NULL(memory,ERROR_NULL_ARG_1); 00041 00042 /* do function */ 00043 free(memory); 00044 00045 return SUCCESS; 00046 } 00047 00048 errorcode wait_for_flag(flag_t *check_flag, flag_t stop_flags, int timeout) { 00049 00050 /* declare local variables */ 00051 00052 /* error check arguments */ 00053 CHECK_NOT_NULL(check_flag,ERROR_NULL_ARG_1); 00054 CHECK_NOT_NEG(timeout,ERROR_NEG_ARG_3); 00055 00056 /* do function */ 00057 do { 00058 if (!((*check_flag)&stop_flags)) { 00059 timeout = DEC_UNTIL_ZERO(timeout); 00060 sleep(1); 00061 } 00062 else { 00063 break; 00064 } 00065 } while (timeout>0); 00066 00067 if (timeout==0) 00068 return ERROR_TIMEOUT; 00069 return SUCCESS; 00070 }