$treeview $search $mathjax $extrastylesheet
librsync
2.3.1
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 * 00003 * librsync -- the library for network deltas 00004 * 00005 * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net> 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU Lesser General Public License as published by 00009 * the Free Software Foundation; either version 2.1 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 00022 /*= 00023 | On heroin, I have all the answers. 00024 */ 00025 00026 #include "config.h" 00027 #include <stdlib.h> 00028 #include <string.h> 00029 #include "librsync.h" 00030 #include "util.h" 00031 #include "trace.h" 00032 00033 void rs_bzero(void *buf, size_t size) 00034 { 00035 memset(buf, 0, size); 00036 } 00037 00038 void *rs_alloc_struct0(size_t size, char const *name) 00039 { 00040 void *p; 00041 00042 if (!(p = malloc(size))) { 00043 rs_fatal("couldn't allocate instance of %s", name); 00044 } 00045 rs_bzero(p, size); 00046 return p; 00047 } 00048 00049 void *rs_alloc(size_t size, char const *name) 00050 { 00051 void *p; 00052 00053 if (!(p = malloc(size))) { 00054 rs_fatal("couldn't allocate instance of %s", name); 00055 } 00056 00057 return p; 00058 } 00059 00060 void *rs_realloc(void *ptr, size_t size, char const *name) 00061 { 00062 void *p; 00063 00064 if (!(p = realloc(ptr, size))) { 00065 rs_fatal("couldn't reallocate instance of %s", name); 00066 } 00067 return p; 00068 } 00069 00070 int rs_long_ln2(rs_long_t v) 00071 { 00072 int n; 00073 00074 /* Count the number of shifts to zero v. */ 00075 for (n = 0; (v >>= 1); n++) ; 00076 return n; 00077 } 00078 00079 int rs_long_sqrt(rs_long_t v) 00080 { 00081 rs_long_t n, b; 00082 00083 /* Find the most significant bit of the root. */ 00084 for (b = 1, n = v; (n >>= 2); b <<= 1) ; 00085 /* Walk down the bits of the root. */ 00086 for (n = 0; b; b >>= 1) { 00087 /* Set the bit in the answer n. */ 00088 n |= b; 00089 /* If n^2 is too big, clear the bit. */ 00090 if (n * n > v) 00091 n ^= b; 00092 } 00093 return n; 00094 }