mirror of
https://github.com/JakeHillion/drgn.git
synced 2024-12-22 17:23:06 +00:00
87b7292aa5
drgn is currently licensed as GPLv3+. Part of the long term vision for drgn is that other projects can use it as a library providing programmatic interfaces for debugger functionality. A more permissive license is better suited to this goal. We decided on LGPLv2.1+ as a good balance between software freedom and permissiveness. All contributors not employed by Meta were contacted via email and consented to the license change. The only exception was the author of commitc4fbf7e589
("libdrgn: fix for compilation error"), who did not respond. That commit reverted a single line of code to one originally written by me in commit640b1c011d
("libdrgn: embed DWARF index in DWARF info cache"). Signed-off-by: Omar Sandoval <osandov@osandov.com>
105 lines
2.2 KiB
C
105 lines
2.2 KiB
C
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "bitops.h"
|
|
#include "string_builder.h"
|
|
#include "util.h"
|
|
|
|
char *string_builder_null_terminate(struct string_builder *sb)
|
|
{
|
|
if (!string_builder_reserve_for_append(sb, 1))
|
|
return NULL;
|
|
sb->str[sb->len] = '\0';
|
|
return sb->str;
|
|
}
|
|
|
|
bool string_builder_reserve(struct string_builder *sb, size_t capacity)
|
|
{
|
|
if (capacity <= sb->capacity)
|
|
return true;
|
|
char *tmp = realloc(sb->str, capacity);
|
|
if (!tmp)
|
|
return false;
|
|
sb->str = tmp;
|
|
sb->capacity = capacity;
|
|
return true;
|
|
}
|
|
|
|
bool string_builder_reserve_for_append(struct string_builder *sb, size_t n)
|
|
{
|
|
if (n == 0)
|
|
return true;
|
|
size_t capacity;
|
|
if (__builtin_add_overflow(sb->len, n, &capacity))
|
|
return false;
|
|
if (capacity < (SIZE_MAX / 2) + 1)
|
|
capacity = next_power_of_two(capacity);
|
|
return string_builder_reserve(sb, capacity);
|
|
}
|
|
|
|
bool string_builder_appendc(struct string_builder *sb, char c)
|
|
{
|
|
if (!string_builder_reserve_for_append(sb, 1))
|
|
return false;
|
|
sb->str[sb->len++] = c;
|
|
return true;
|
|
}
|
|
|
|
bool string_builder_appendn(struct string_builder *sb, const char *str,
|
|
size_t len)
|
|
{
|
|
if (!string_builder_reserve_for_append(sb, len))
|
|
return false;
|
|
memcpy(&sb->str[sb->len], str, len);
|
|
sb->len += len;
|
|
return true;
|
|
}
|
|
|
|
bool string_builder_vappendf(struct string_builder *sb, const char *format,
|
|
va_list ap)
|
|
{
|
|
va_list aq;
|
|
int len;
|
|
|
|
again:
|
|
va_copy(aq, ap);
|
|
len = vsnprintf(add_to_possibly_null_pointer(sb->str, sb->len),
|
|
sb->capacity - sb->len, format, aq);
|
|
va_end(aq);
|
|
if (len < 0)
|
|
return false;
|
|
if (sb->len + len < sb->capacity) {
|
|
sb->len += len;
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* vsnprintf() always null-terminates the string, so we have to allocate
|
|
* an extra character.
|
|
*/
|
|
if (!string_builder_reserve_for_append(sb, len + 1))
|
|
return false;
|
|
goto again;
|
|
}
|
|
|
|
bool string_builder_appendf(struct string_builder *sb, const char *format, ...)
|
|
{
|
|
va_list ap;
|
|
bool ret;
|
|
|
|
va_start(ap, format);
|
|
ret = string_builder_vappendf(sb, format, ap);
|
|
va_end(ap);
|
|
return ret;
|
|
}
|
|
|
|
bool string_builder_line_break(struct string_builder *sb)
|
|
{
|
|
if (!sb->len || sb->str[sb->len - 1] == '\n')
|
|
return true;
|
|
return string_builder_appendc(sb, '\n');
|
|
}
|