librsync  2.3.3
util.c
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- the library for network deltas
4 *
5 * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /*=
23 | On heroin, I have all the answers.
24 */
25
26#include <stdlib.h>
27#include <string.h>
28#include "librsync.h"
29#include "util.h"
30#include "trace.h"
31
32void rs_bzero(void *buf, size_t size)
33{
34 memset(buf, 0, size);
35}
36
37void *rs_alloc_struct0(size_t size, char const *name)
38{
39 void *p;
40
41 if (!(p = malloc(size))) {
42 rs_fatal("couldn't allocate instance of %s", name);
43 }
44 rs_bzero(p, size);
45 return p;
46}
47
48void *rs_alloc(size_t size, char const *name)
49{
50 void *p;
51
52 if (!(p = malloc(size))) {
53 rs_fatal("couldn't allocate instance of %s", name);
54 }
55
56 return p;
57}
58
59void *rs_realloc(void *ptr, size_t size, char const *name)
60{
61 void *p;
62
63 if (!(p = realloc(ptr, size))) {
64 rs_fatal("couldn't reallocate instance of %s", name);
65 }
66 return p;
67}
68
69int rs_long_ln2(rs_long_t v)
70{
71 int n;
72
73 /* Count the number of shifts to zero v. */
74 for (n = 0; (v >>= 1); n++) ;
75 return n;
76}
77
78int rs_long_sqrt(rs_long_t v)
79{
80 rs_long_t n, b;
81
82 /* Find the most significant bit of the root. */
83 for (b = 1, n = v; (n >>= 2); b <<= 1) ;
84 /* Walk down the bits of the root. */
85 for (n = 0; b; b >>= 1) {
86 /* Set the bit in the answer n. */
87 n |= b;
88 /* If n^2 is too big, clear the bit. */
89 if (n * n > v)
90 n ^= b;
91 }
92 return (int)n;
93}
Public header for librsync.
logging functions.
Misc utility functions used by librsync.