librsync  2.3.4
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 "config.h" /* IWYU pragma: keep */
27#include <stdlib.h>
28#include <string.h>
29#include "librsync.h"
30#include "util.h"
31#include "trace.h"
32
33void rs_bzero(void *buf, size_t size)
34{
35 memset(buf, 0, size);
36}
37
38void *rs_alloc_struct0(size_t size, char const *name)
39{
40 void *p;
41
42 if (!(p = malloc(size))) {
43 rs_fatal("couldn't allocate instance of %s", name);
44 }
45 rs_bzero(p, size);
46 return p;
47}
48
49void *rs_alloc(size_t size, char const *name)
50{
51 void *p;
52
53 if (!(p = malloc(size))) {
54 rs_fatal("couldn't allocate instance of %s", name);
55 }
56
57 return p;
58}
59
60void *rs_realloc(void *ptr, size_t size, char const *name)
61{
62 void *p;
63
64 if (!(p = realloc(ptr, size))) {
65 rs_fatal("couldn't reallocate instance of %s", name);
66 }
67 return p;
68}
69
70int rs_long_ln2(rs_long_t v)
71{
72 int n;
73
74 /* Count the number of shifts to zero v. */
75 for (n = 0; (v >>= 1); n++) ;
76 return n;
77}
78
79int rs_long_sqrt(rs_long_t v)
80{
81 rs_long_t n, b;
82
83 /* Find the most significant bit of the root. */
84 for (b = 1, n = v; (n >>= 2); b <<= 1) ;
85 /* Walk down the bits of the root. */
86 for (n = 0; b; b >>= 1) {
87 /* Set the bit in the answer n. */
88 n |= b;
89 /* If n^2 is too big, clear the bit. */
90 if (n * n > v)
91 n ^= b;
92 }
93 return (int)n;
94}
Public header for librsync.
logging functions.
Misc utility functions used by librsync.