1 /*- 2 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 #include <sys/types.h> 26 #include <sys/param.h> 27 #include <sys/syslog.h> 28 29 #ifdef _KERNEL 30 # include <sys/kmem.h> 31 #else 32 # include <ctype.h> 33 # include <inttypes.h> 34 # include <stdarg.h> 35 # include <stdio.h> 36 # include <stdlib.h> 37 # include <string.h> 38 # include <time.h> 39 # include <unistd.h> 40 #endif 41 42 #include "misc.h" 43 44 #ifndef USE_ARG 45 #define USE_ARG(x) /*LINTED*/(void)&(x) 46 #endif 47 48 void * 49 netpgp_allocate(size_t n, size_t nels) 50 { 51 #ifdef _KERNEL 52 return kmem_zalloc(n * nels, KM_SLEEP); 53 #else 54 return calloc(n, nels); 55 #endif 56 } 57 58 void 59 netpgp_deallocate(void *ptr, size_t size) 60 { 61 #ifdef _KERNEL 62 kmem_free(ptr, size); 63 #else 64 USE_ARG(size); 65 free(ptr); 66 #endif 67 } 68 69 #ifndef _KERNEL 70 void 71 logmessage(const int level, const char *fmt, ...) 72 { 73 va_list args; 74 75 USE_ARG(level); 76 if (fmt != NULL) { 77 va_start(args, fmt); 78 vfprintf(stderr, fmt, args); 79 va_end(args); 80 } 81 } 82 #endif 83