1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*++
Module Name:
Util.cpp
Abstract:
Generic support routines that don't seem to belong elsewhere.
Authors:
Bill Bolosky, March, 2013
Environment:
User mode service.
Revision History:
Factored from other places
--*/
#include "stdafx.h"
#include "Util.h"
#include "Error.h"
#include "GenericFile.h"
#include "DataReader.h"
_int64 FirstPowerOf2GreaterThanOrEqualTo(_int64 value)
{
int highestBitSet;
for (highestBitSet = 0; highestBitSet <= 62; highestBitSet++) { // Only go to 63, since this is signed
if (!(value & ~((((_int64)1) << highestBitSet) - 1))) {
highestBitSet -= 1;
break;
}
}
if (((_int64)1) << highestBitSet == value) return value;
return ((_int64)1) << (highestBitSet + 1);
}
int cheezyLogBase2(_int64 value)
{
int retVal = 0;
value /= 2; // Since 2^0 = 1; we'll also define cheezyLogBase2(x) = 0 where x<= 0.
while (value > 0) {
retVal++;
value /= 2;
}
return retVal;
}
void
util::memrevcpy(
void* dst,
const void* src,
size_t bytes)
{
size_t dwords = bytes >> 3;
_uint64* p = (_uint64*) dst;
const _uint64* q = (const _uint64*) ((const char*)src + bytes - 8);
for (size_t i = 0; i < dwords; i++) {
*p++ = ByteSwapUI64(*q--);
}
int left = (int) (bytes & 7);
if (left > 0) {
char* p2 = (char*) p;
const char* q2 = (left - 1) + (const char*) src;
for (int i = 0; i < left; i++) {
*p2++ = *q2--;
}
}
}
NWaiter::NWaiter(size_t n)
{
_signalsRequired = n;
_signalsReceived = 0;
InitializeExclusiveLock(&_lock);
CreateEventObject(&_waiter);
}
NWaiter::~NWaiter()
{
DestroyExclusiveLock(&_lock);
DestroyEventObject(&_waiter);
}
void NWaiter::wait()
{
while (true) {
bool done;
AcquireExclusiveLock(&_lock);
done = (_signalsReceived >= _signalsRequired);
ReleaseExclusiveLock(&_lock);
if (done)
return;
else {
WaitForEvent(&_waiter);
}
}
}
void NWaiter::signal()
{
AcquireExclusiveLock(&_lock);
_signalsReceived += 1;
ReleaseExclusiveLock(&_lock);
AllowEventWaitersToProceed(&_waiter);
}
char *FormatUIntWithCommas(_uint64 val, char *outputBuffer, size_t outputBufferSize, size_t desiredLength)
{
_ASSERT(desiredLength < outputBufferSize);
//
// First, figure out the number of digits.
//
unsigned nDigits = 0;
_uint64 x = val;
while (x > 0) {
nDigits++;
x = x / 10;
}
if (0 == nDigits) {
//
// Special case for the value 0 (which, I suppose if the world was rational, would be represented by the empty string. :-))
//
_ASSERT(0 == val);
nDigits = 1;
}
int nCommas = (nDigits - 1) / 3;
if (outputBufferSize < (size_t)nDigits + nCommas + 1) {
WriteErrorMessage("Internal error: too small buffer for FormatUIntWithCommas, value %lld, outputBufferSize %lld\n", val, outputBufferSize);
if (outputBufferSize > 0) {
*outputBuffer = 0;
} else {
soft_exit(1);
}
return outputBuffer;
}
//
// Now build up the string backwards.
//
size_t offset = (size_t)nDigits + nCommas;
outputBuffer[offset] = '\0';
if (0 == val) {
outputBuffer[0] = '0';
return outputBuffer;
}
x = val;
while (x > 0) {
char tempBuf[5];
if (x > 999) {
sprintf(tempBuf, ",%03lld", x % 1000);
_ASSERT(strlen(tempBuf) == 4);
} else {
sprintf(tempBuf, "%lld", x);
}
_ASSERT(offset >= strlen(tempBuf));
offset -= strlen(tempBuf);
memcpy(outputBuffer + offset, tempBuf, strlen(tempBuf));
x /= 1000;
}
for (size_t i = strlen(outputBuffer); i < desiredLength; i++) {
strcat(outputBuffer, " ");
}
return outputBuffer;
}
//
// Version of fgets that dynamically (re-)allocates the buffer to be big enough to fit the whole line
//
class FgetsObject
{
public:
virtual char *fgets(char *s, int size) = 0;
};
char *genericReallocatingFgets(char **buffer, int *io_bufferSize, FgetsObject *getsObject)
{
if (*io_bufferSize == 0) {
//
// Just pick a decent size to start out with.
//
*io_bufferSize = 128;
*buffer = new char[*io_bufferSize];
}
int offset = 0;
for (;;) { // loop with middle exit
if (NULL == getsObject->fgets((*buffer) + offset, *io_bufferSize - offset)) { // Recall that if this clips, then the next time it just keeps reading from the clip.
//
// EOF or error.
//
return NULL;
}
int len = (int)strlen(*buffer);
if (len != *io_bufferSize - 1 || (*buffer)[len - 1] == '\n') {
//
// It fit.
//
return *buffer;
}
//
// Double the buffer and retry.
//
if (((_int64)*io_bufferSize) * 2 > 0x7fffffff) {
WriteErrorMessage("Trying to fgets() a string bigger than 2^31 bytes. Perhaps you've supplied an incorrect input file.");
soft_exit(1);
}
int newBufferSize = *io_bufferSize * 2;
char *newBuffer = new char[newBufferSize];
offset = len;
memcpy(newBuffer, *buffer, offset);
delete[] *buffer;
*buffer = newBuffer;
*io_bufferSize = newBufferSize;
}
//NOTREACHED
return NULL; // Just to keep the compiler happy
}
class StdioFGetsObject : public FgetsObject
{
public:
StdioFGetsObject(FILE *_file) : file(_file) {}
virtual char *fgets(char *s, int size) {
return ::fgets(s, size, file);
}
private:
FILE *file;
};
char *reallocatingFgets(char **buffer, int *io_bufferSize, FILE *stream)
{
StdioFGetsObject fgetsObject(stream);
return genericReallocatingFgets(buffer, io_bufferSize, &fgetsObject);
}
class GenericFileFGetsObject : public FgetsObject
{
public:
GenericFileFGetsObject(GenericFile *_file) : file(_file) {}
virtual char *fgets(char *s, int size) {
return file->gets(s, size);
}
private:
GenericFile *file;
};
char *reallocatingFgetsGenericFile(char **buffer, int *io_bufferSize, GenericFile *file)
{
GenericFileFGetsObject fgetsObject(file);
return genericReallocatingFgets(buffer, io_bufferSize, &fgetsObject);
}
class DataReaderFGetsObject : public FgetsObject
{
public:
DataReaderFGetsObject(DataReader* _dataReader) : dataReader(_dataReader) {}
virtual char* fgets(char* s, int size) {
char* buffer = NULL;
_int64 validBytes = 0;
_int64 offsetInBuffer = 0;
int bytesCopied = 0;
int amountAlreadyAdvanced = 0;
while (bytesCopied < size - 1) {
if (offsetInBuffer >= validBytes) {
dataReader->advance(offsetInBuffer);
if (!dataReader->getData(&buffer, &validBytes)) {
if (dataReader->isEOF()) {
if (bytesCopied != 0) {
s[bytesCopied] = '\0';
return s;
}
return NULL;
}
dataReader->nextBatch();
if (!dataReader->getData(&buffer, &validBytes)) {
if (dataReader->isEOF()) {
if (bytesCopied != 0) {
s[bytesCopied] = '\0';
return s;
}
return NULL;
}
WriteErrorMessage("DataReaderFGetsObject: getData failed not at EOF\n");
soft_exit(1);
} // getData failed (twice)
offsetInBuffer = 0;
} // getData failed (once)
} // If we're past the end of the buffer
s[bytesCopied] = buffer[offsetInBuffer];
offsetInBuffer++;
bytesCopied++;
if (s[bytesCopied-1] == '\n') {
break;
}
} // while we haven't run out of space to copy into (or hit a newline, which exits in the middle of the loop)
s[bytesCopied] = '\0';
dataReader->advance(offsetInBuffer);
return s;
} // fgets
private:
DataReader* dataReader;
};
char* reallocatingFgets(char** buffer, int* io_bufferSize, DataReader* stream)
{
DataReaderFGetsObject fgetsObject(stream);
return genericReallocatingFgets(buffer, io_bufferSize, &fgetsObject);
}
bool isFilenameGzip(const char* filename)
{
return util::stringEndsWith(filename, ".gz") || util::stringEndsWith(filename, ".gzip");
}
DataReader* getDefaultOrGzipDataReader(const char* filename)
{
DataSupplier* dataSupplier;
size_t filenameLen = strlen(filename);
if (isFilenameGzip(filename)) {
dataSupplier = DataSupplier::GzipDefault;
} else {
dataSupplier = DataSupplier::Default;
}
DataReader* dataReader = dataSupplier->getDataReader(3, 1024, 10.0, 1024 * 1024);
if (!dataReader->init(filename)) {
delete dataReader;
return NULL;
}
dataReader->reinit(0, 0);
return dataReader;
} // getDefaultOrGzipDataReader
|