\doxysubsection{util.\+c}
\label{util_8c_source}\index{util.c@{util.c}}

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

\end{DoxyCode}
