mirror of
https://github.com/JakeHillion/drgn.git
synced 2024-12-23 17:53:07 +00:00
75c3679147
The current mixed Python/C implementation works well, but it has a couple of important limitations: - It's too slow for some common use cases, like iterating over large data structures. - It can't be reused in utilities written in other languages. This replaces the internals with a new library written in C, libdrgn. It includes Python bindings with mostly the same public interface as before, with some important improvements: - Types are now represented by a single Type class rather than the messy polymorphism in the Python implementation. - Qualifiers are a bitmask instead of a set of strings. - Bit fields are not considered a separate type. - The lvalue/rvalue terminology is replaced with reference/value. - Structure, union, and array values are better supported. - Function objects are supported. - Program distinguishes between lookups of variables, constants, and functions. The C rewrite is about 6x as fast as the original Python when using the Python bindings, and about 8x when using the C API directly. Currently, the exposed API in C is fairly conservative. In the future, the memory reader, type index, and object index APIs will probably be exposed for more flexibility.
105 lines
2.1 KiB
C
105 lines
2.1 KiB
C
// Copyright 2018-2019 - Omar Sandoval
|
|
// SPDX-License-Identifier: GPL-3.0+
|
|
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#include "internal.h"
|
|
#include "string_builder.h"
|
|
|
|
struct drgn_error *string_builder_init(struct string_builder *sb)
|
|
{
|
|
sb->len = 0;
|
|
sb->capacity = 16;
|
|
sb->str = malloc(sb->capacity);
|
|
if (!sb->str)
|
|
return &drgn_enomem;
|
|
sb->str[0] = '\0';
|
|
return NULL;
|
|
}
|
|
|
|
struct drgn_error *string_builder_reserve(struct string_builder *sb,
|
|
size_t capacity)
|
|
{
|
|
size_t new_capacity = sb->capacity;
|
|
char *tmp;
|
|
|
|
if (capacity < new_capacity)
|
|
return NULL;
|
|
|
|
while (capacity >= new_capacity)
|
|
new_capacity *= 2;
|
|
tmp = realloc(sb->str, new_capacity);
|
|
if (!tmp)
|
|
return &drgn_enomem;
|
|
sb->str = tmp;
|
|
sb->capacity = new_capacity;
|
|
return NULL;
|
|
}
|
|
|
|
struct drgn_error *string_builder_appendc(struct string_builder *sb, char c)
|
|
{
|
|
struct drgn_error *err;
|
|
|
|
err = string_builder_reserve(sb, sb->len + 1);
|
|
if (err)
|
|
return err;
|
|
sb->str[sb->len++] = c;
|
|
sb->str[sb->len] = '\0';
|
|
return NULL;
|
|
}
|
|
|
|
struct drgn_error *string_builder_appendn(struct string_builder *sb,
|
|
const char *str,
|
|
size_t len)
|
|
{
|
|
struct drgn_error *err;
|
|
|
|
err = string_builder_reserve(sb, sb->len + len);
|
|
if (err)
|
|
return err;
|
|
memcpy(&sb->str[sb->len], str, len);
|
|
sb->len += len;
|
|
sb->str[sb->len] = '\0';
|
|
return NULL;
|
|
}
|
|
|
|
struct drgn_error *string_builder_vappendf(struct string_builder *sb,
|
|
const char *format,
|
|
va_list ap)
|
|
{
|
|
struct drgn_error *err;
|
|
va_list aq;
|
|
int len;
|
|
|
|
again:
|
|
va_copy(aq, ap);
|
|
len = vsnprintf(&sb->str[sb->len], sb->capacity - sb->len, format, aq);
|
|
va_end(aq);
|
|
if (len < 0)
|
|
return drgn_error_create_os(errno, NULL, "vsnprintf");
|
|
if (sb->len + len < sb->capacity) {
|
|
sb->len += len;
|
|
sb->str[sb->len] = '\0';
|
|
return NULL;
|
|
}
|
|
|
|
err = string_builder_reserve(sb, sb->len + len);
|
|
if (err)
|
|
return err;
|
|
goto again;
|
|
}
|
|
|
|
struct drgn_error *string_builder_appendf(struct string_builder *sb,
|
|
const char *format, ...)
|
|
{
|
|
struct drgn_error *err;
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
err = string_builder_vappendf(sb, format, ap);
|
|
va_end(ap);
|
|
return err;
|
|
}
|