opt: optimize project struct

This commit is contained in:
DreamMaoMao
2025-04-28 09:19:26 +08:00
parent f2dbbbb8f2
commit cd78449180
11 changed files with 9 additions and 9 deletions

48
src/common/mem.c Normal file
View File

@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Based on labwc (https://github.com/labwc/labwc) */
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "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;
}

64
src/common/mem.h Normal file
View File

@@ -0,0 +1,64 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Based on labwc (https://github.com/labwc/labwc) */
#ifndef LABWC_MEM_H
#define LABWC_MEM_H
#include <stdlib.h>
/*
* As defined in busybox, weston, etc.
* Allocates zero-filled memory; calls exit() on error.
* Returns NULL only if (size == 0).
*/
void *xzalloc(size_t size);
/*
* Type-safe macros in the style of C++ new/new[].
* Both allocate zero-filled memory for object(s) the same size as
* <expr>, which may be either a type name or value expression.
*
* znew() allocates space for one object.
* znew_n() allocates space for an array of <n> objects.
*
* Examples:
* struct wlr_box *box = znew(*box);
* char *buf = znew_n(char, 80);
*/
#define znew(expr) ((__typeof__(expr) *)xzalloc(sizeof(expr)))
#define znew_n(expr, n) ((__typeof__(expr) *)xzalloc((n) * sizeof(expr)))
/*
* As defined in FreeBSD.
* Like realloc(), but calls exit() on error.
* Returns NULL only if (size == 0).
* Does NOT zero-fill memory.
*/
void *xrealloc(void *ptr, size_t size);
/* malloc() is a specific case of realloc() */
#define xmalloc(size) xrealloc(NULL, (size))
/*
* As defined in FreeBSD.
* Allocates a copy of <str>; calls exit() on error.
* Requires (str != NULL) and never returns NULL.
*/
char *xstrdup(const char *str);
/*
* Same as ptr = xstrdup(str) but free
* <ptr> before assigning the result.
*/
#define xstrdup_replace(ptr, str) do { \
free(ptr); (ptr) = xstrdup(str); \
} while (0)
/*
* Frees memory pointed to by <ptr> and sets <ptr> to NULL.
* Does nothing if <ptr> is already NULL.
*/
#define zfree(ptr) do { \
free(ptr); (ptr) = NULL; \
} while (0)
#endif /* LABWC_MEM_H */

51
src/common/util.c Normal file
View File

@@ -0,0 +1,51 @@
/* See LICENSE.dwm file for copyright and license details. */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "util.h"
void
die(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
exit(1);
}
void *
ecalloc(size_t nmemb, size_t size)
{
void *p;
if (!(p = calloc(nmemb, size)))
die("calloc:");
return p;
}
int
fd_set_nonblock(int fd) {
int flags = fcntl(fd, F_GETFL);
if (flags < 0) {
perror("fcntl(F_GETFL):");
return -1;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
perror("fcntl(F_SETFL):");
return -1;
}
return 0;
}

5
src/common/util.h Normal file
View File

@@ -0,0 +1,5 @@
/* See LICENSE.dwm file for copyright and license details. */
void die(const char *fmt, ...);
void *ecalloc(size_t nmemb, size_t size);
int fd_set_nonblock(int fd);