00001 // Copyright (C) 2001, Compaq Computer Corporation 00002 00003 // This file is part of Vesta. 00004 00005 // Vesta is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU Lesser General Public 00007 // License as published by the Free Software Foundation; either 00008 // version 2.1 of the License, or (at your option) any later version. 00009 00010 // Vesta is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // Lesser General Public License for more details. 00014 00015 // You should have received a copy of the GNU Lesser General Public 00016 // License along with Vesta; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 // Last modified on Wed Nov 7 14:40:45 EST 2001 by ken@xorian.net 00020 00021 // Define an assertion that allows us to check the result of the 00022 // sizeof operator. This helps identify portability problems with 00023 // size type differences between platforms. 00024 00025 #ifndef __SizeAssert 00026 #define __SizeAssert 00027 00028 // This function will be called in the result of a mismatch between 00029 // the actual and expected data size. 00030 extern void sizeof_assert_fail(const char * typ, 00031 unsigned long gotsize, unsigned long targsize, 00032 const char * fname, unsigned int line, 00033 bool die); 00034 00035 // This is the macro that other code should use. By default, a 00036 // message is printed but execution continues. To force a failure on 00037 // a size mismatch, define the SIZE_ASSERT_ABORT macro before this 00038 // file is included (e.g. on the command line). 00039 00040 #ifdef SIZE_ASSERT_ABORT 00041 #define sizeof_assert(a, b) \ 00042 ((sizeof(a) == (b)) ? (b) : \ 00043 (sizeof_assert_fail(#a, sizeof(a), b, __FILE__, __LINE__, true), \ 00044 sizeof(a))) 00045 #else 00046 #define sizeof_assert(a, b) \ 00047 ((sizeof(a) == (b)) ? (b) : \ 00048 (sizeof_assert_fail(#a, sizeof(a), b, __FILE__, __LINE__, false), \ 00049 sizeof(a))) 00050 #endif 00051 00052 // Example use: 00053 // 00054 // write(fd, &data, sizeof_assert(data, 4)) 00055 // 00056 // If we think data is 4 bytes 00057 00058 #endif // __SizeAssert