sapling/lib/clib/buffer.h
Durham Goode 0938fe19a3 clib: move fb-hgext/clib/ to lib
Summary:
cdatapack depends on clib, so let's move it to lib/ outside of fb-hgext.

None of the consumers of these files were changed. They will be changed as they
are moved into the main part of the repo.

Test Plan: hg purge --all && make local && cd tests && ./run-tests.py -S -j 48

Reviewers: mitrandir, #mercurial

Reviewed By: mitrandir

Differential Revision: https://phabricator.intern.facebook.com/D6677197

Signature: 6677197:1515447873:399fb3e7beb5cc1ad8db18f42b359ffbfbeb21f2
2018-01-08 15:08:18 -08:00

58 lines
1.7 KiB
C

// Copyright (c) 2004-present, Facebook, Inc.
// All Rights Reserved.
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
// buffer.c: declarations for a generic mechanism to expand a heap-allocated
// buffer. this is for internal use only.
// no-check-code
#ifndef FBHGEXT_CLIB_BUFFER_H
#define FBHGEXT_CLIB_BUFFER_H
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
static inline bool expand_to_fit(
void **buffer, size_t num_slots_used, size_t *num_slots_total,
size_t input_count, size_t item_sz,
const float factor,
const size_t min_increment,
const size_t max_increment) {
size_t remaining = *num_slots_total - num_slots_used;
if (input_count > remaining) {
// need realloc
size_t new_slots_total = factor * ((float) *num_slots_total);
if (new_slots_total < min_increment + *num_slots_total) {
new_slots_total = min_increment + *num_slots_total;
}
if (new_slots_total > max_increment + *num_slots_total) {
new_slots_total = max_increment + *num_slots_total;
}
if (new_slots_total < input_count + *num_slots_total) {
new_slots_total = input_count + *num_slots_total;
}
void *newbuffer = realloc(*buffer, item_sz * new_slots_total);
if (newbuffer == NULL) {
return false;
}
*buffer = newbuffer;
*num_slots_total = new_slots_total;
}
return true;
}
extern bool buffer_append(
char **buffer, size_t *buffer_idx, size_t *buffer_sz,
char *input, size_t input_sz,
const float factor,
const size_t min_increment,
const size_t max_increment);
#endif /* FBHGEXT_CLIB_BUFFER_H */