mirror of
https://github.com/labwc/labwc.git
synced 2025-11-04 13:30:07 -05:00
common: Add additional memory utilities (xzalloc() etc.)
This commit is contained in:
parent
b89f7bfc0d
commit
cb40cdc36c
35 changed files with 193 additions and 167 deletions
|
|
@ -1,6 +1,7 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
#include <ctype.h>
|
||||
#include "common/buf.h"
|
||||
#include "common/mem.h"
|
||||
|
||||
static void
|
||||
strip_curly_braces(char *s)
|
||||
|
|
@ -45,7 +46,7 @@ buf_expand_shell_variables(struct buf *s)
|
|||
/* just add one character at a time */
|
||||
if (new.alloc <= new.len + 1) {
|
||||
new.alloc = new.alloc * 3 / 2 + 16;
|
||||
new.buf = realloc(new.buf, new.alloc);
|
||||
new.buf = xrealloc(new.buf, new.alloc);
|
||||
}
|
||||
new.buf[new.len++] = s->buf[i];
|
||||
new.buf[new.len] = '\0';
|
||||
|
|
@ -60,7 +61,7 @@ void
|
|||
buf_init(struct buf *s)
|
||||
{
|
||||
s->alloc = 256;
|
||||
s->buf = malloc(s->alloc);
|
||||
s->buf = xmalloc(s->alloc);
|
||||
s->buf[0] = '\0';
|
||||
s->len = 0;
|
||||
}
|
||||
|
|
@ -74,7 +75,7 @@ buf_add(struct buf *s, const char *data)
|
|||
int len = strlen(data);
|
||||
if (s->alloc <= s->len + len + 1) {
|
||||
s->alloc = s->alloc + len;
|
||||
s->buf = realloc(s->buf, s->alloc);
|
||||
s->buf = xrealloc(s->buf, s->alloc);
|
||||
}
|
||||
memcpy(s->buf + s->len, data, len);
|
||||
s->len += len;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <wlr/types/wlr_scene.h>
|
||||
#include "common/graphic-helpers.h"
|
||||
#include "common/mem.h"
|
||||
|
||||
static void
|
||||
multi_rect_destroy_notify(struct wl_listener *listener, void *data)
|
||||
|
|
@ -16,7 +17,7 @@ multi_rect_destroy_notify(struct wl_listener *listener, void *data)
|
|||
struct multi_rect *
|
||||
multi_rect_create(struct wlr_scene_tree *parent, float *colors[3], int line_width)
|
||||
{
|
||||
struct multi_rect *rect = calloc(1, sizeof(*rect));
|
||||
struct multi_rect *rect = xzalloc(sizeof(*rect));
|
||||
rect->line_width = line_width;
|
||||
rect->tree = wlr_scene_tree_create(parent);
|
||||
rect->destroy.notify = multi_rect_destroy_notify;
|
||||
|
|
|
|||
47
src/common/mem.c
Normal file
47
src/common/mem.c
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "common/mem.h"
|
||||
|
||||
static void
|
||||
die_if_null(void *ptr)
|
||||
{
|
||||
if (!ptr) {
|
||||
perror("Failed to allocate memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void *
|
||||
xzalloc(size_t size)
|
||||
{
|
||||
if (!size) {
|
||||
return NULL;
|
||||
}
|
||||
void *ptr = calloc(1, size);
|
||||
die_if_null(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *
|
||||
xrealloc(void *ptr, size_t size)
|
||||
{
|
||||
if (!size) {
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
ptr = realloc(ptr, size);
|
||||
die_if_null(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
char *
|
||||
xstrdup(const char *str)
|
||||
{
|
||||
assert(str);
|
||||
char *copy = strdup(str);
|
||||
die_if_null(copy);
|
||||
return copy;
|
||||
}
|
||||
|
|
@ -5,11 +5,11 @@ labwc_sources += files(
|
|||
'font.c',
|
||||
'grab-file.c',
|
||||
'graphic-helpers.c',
|
||||
'mem.c',
|
||||
'nodename.c',
|
||||
'scaled_font_buffer.c',
|
||||
'scaled_scene_buffer.c',
|
||||
'scene-helpers.c',
|
||||
'spawn.c',
|
||||
'string-helpers.c',
|
||||
'zfree.c',
|
||||
)
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@
|
|||
#include <wlr/util/log.h>
|
||||
#include "buffer.h"
|
||||
#include "common/font.h"
|
||||
#include "common/mem.h"
|
||||
#include "common/scaled_scene_buffer.h"
|
||||
#include "common/scaled_font_buffer.h"
|
||||
#include "common/zfree.h"
|
||||
|
||||
static struct lab_data_buffer *
|
||||
_create_buffer(struct scaled_scene_buffer *scaled_buffer, double scale)
|
||||
|
|
@ -46,10 +46,7 @@ struct scaled_font_buffer *
|
|||
scaled_font_buffer_create(struct wlr_scene_tree *parent)
|
||||
{
|
||||
assert(parent);
|
||||
struct scaled_font_buffer *self = calloc(1, sizeof(*self));
|
||||
if (!self) {
|
||||
return NULL;
|
||||
}
|
||||
struct scaled_font_buffer *self = xzalloc(sizeof(*self));
|
||||
|
||||
struct scaled_scene_buffer *scaled_buffer
|
||||
= scaled_scene_buffer_create(parent, &impl);
|
||||
|
|
@ -80,16 +77,16 @@ scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
|
|||
zfree(self->arrow);
|
||||
|
||||
/* Update internal state */
|
||||
self->text = strdup(text);
|
||||
self->text = xstrdup(text);
|
||||
self->max_width = max_width;
|
||||
if (font->name) {
|
||||
self->font.name = strdup(font->name);
|
||||
self->font.name = xstrdup(font->name);
|
||||
}
|
||||
self->font.size = font->size;
|
||||
self->font.slant = font->slant;
|
||||
self->font.weight = font->weight;
|
||||
memcpy(self->color, color, sizeof(self->color));
|
||||
self->arrow = arrow ? strdup(arrow) : NULL;
|
||||
self->arrow = arrow ? xstrdup(arrow) : NULL;
|
||||
|
||||
/* Invalidate cache and force a new render */
|
||||
scaled_scene_buffer_invalidate_cache(self->scaled_buffer);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
#include <wlr/types/wlr_scene.h>
|
||||
#include <wlr/util/log.h>
|
||||
#include "buffer.h"
|
||||
#include "common/mem.h"
|
||||
#include "common/scaled_scene_buffer.h"
|
||||
|
||||
/**
|
||||
|
|
@ -67,7 +68,7 @@ _update_buffer(struct scaled_scene_buffer *self, double scale)
|
|||
|
||||
/* Create or reuse cache entry */
|
||||
if (wl_list_length(&self->cache) < LAB_SCALED_BUFFER_MAX_CACHE) {
|
||||
cache_entry = calloc(1, sizeof(*cache_entry));
|
||||
cache_entry = xzalloc(sizeof(*cache_entry));
|
||||
} else {
|
||||
cache_entry = wl_container_of(self->cache.prev, cache_entry, link);
|
||||
if (cache_entry->buffer) {
|
||||
|
|
@ -150,10 +151,7 @@ scaled_scene_buffer_create(struct wlr_scene_tree *parent,
|
|||
assert(impl);
|
||||
assert(impl->create_buffer);
|
||||
|
||||
struct scaled_scene_buffer *self = calloc(1, sizeof(*self));
|
||||
if (!self) {
|
||||
return NULL;
|
||||
}
|
||||
struct scaled_scene_buffer *self = xzalloc(sizeof(*self));
|
||||
|
||||
self->scene_buffer = wlr_scene_buffer_create(parent, NULL);
|
||||
if (!self->scene_buffer) {
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
#include <stdlib.h>
|
||||
#include "common/zfree.h"
|
||||
|
||||
void __zfree(void **ptr)
|
||||
{
|
||||
if (!ptr || !*ptr) {
|
||||
return;
|
||||
}
|
||||
free(*ptr);
|
||||
*ptr = NULL;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue