mirror of
https://github.com/javalsai/lidm.git
synced 2025-07-03 14:25:03 +02:00
Merge branch 'master' into code-formatting
This commit is contained in:
commit
d6a348c543
5
.github/workflows/push.yml
vendored
5
.github/workflows/push.yml
vendored
@ -1,7 +1,10 @@
|
|||||||
name: Push Checks
|
name: Push Checks
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push
|
push:
|
||||||
|
pull_request:
|
||||||
|
types:
|
||||||
|
- opened
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check-and-build:
|
check-and-build:
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
pkgbase = lidm-bin
|
pkgbase = lidm-bin
|
||||||
pkgdesc = A fully colorful customizable TUI display manager made in C. (release binary)
|
pkgdesc = A fully colorful customizable TUI display manager made in C. (release binary)
|
||||||
pkgver = 0.2.1
|
pkgver = 0.2.2
|
||||||
pkgrel = 2
|
pkgrel = 1
|
||||||
url = https://github.com/javalsai/lidm
|
url = https://github.com/javalsai/lidm
|
||||||
arch = x86_64
|
arch = x86_64
|
||||||
license = GPL
|
license = GPL
|
||||||
depends = pam
|
depends = pam
|
||||||
provides = lidm
|
provides = lidm
|
||||||
conflicts = lidm
|
conflicts = lidm
|
||||||
source = lidm::https://github.com/javalsai/lidm/releases/download/v0.2.1/lidm-amd64
|
source = lidm::https://github.com/javalsai/lidm/releases/download/v0.2.2/lidm-amd64
|
||||||
source = default-theme.ini::https://raw.githubusercontent.com/javalsai/lidm/v0.2.1/themes/default.ini
|
source = default-theme.ini::https://raw.githubusercontent.com/javalsai/lidm/v0.2.2/themes/default.ini
|
||||||
source = lidm.1::https://raw.githubusercontent.com/javalsai/lidm/v0.2.1/assets/man/lidm.1
|
source = lidm.1::https://raw.githubusercontent.com/javalsai/lidm/v0.2.2/assets/man/lidm.1
|
||||||
source = lidm-config.5::https://raw.githubusercontent.com/javalsai/lidm/v0.2.1/assets/man/lidm-config.5
|
source = lidm-config.5::https://raw.githubusercontent.com/javalsai/lidm/v0.2.2/assets/man/lidm-config.5
|
||||||
sha256sums = 4969018d527613729336abd51e37283ce77d7c7a2233434642804b88e550e622
|
sha256sums = be2307be9bef7b3ef294fb0e8178040e2d8ccff6b8efb7546514da8b0f69c179
|
||||||
sha256sums = 27db9b0cd2da80c0c60dcb13dfad0f9d65e7dddbb7b344b859803b9ac3943cd7
|
sha256sums = 68662430a6d262b35cc54d9f0e164ed935b7f7f4497a87cc94946c558bbe8a91
|
||||||
sha256sums = a6807a55ff72ec5a5678583156b3efd0d367f0bcb79854094132771f0cb86bce
|
sha256sums = a6807a55ff72ec5a5678583156b3efd0d367f0bcb79854094132771f0cb86bce
|
||||||
sha256sums = 3adaae60f79dff1cef2b2aba7dcea04196cd49816759ad36afb9f7331ac9c3e4
|
sha256sums = 3adaae60f79dff1cef2b2aba7dcea04196cd49816759ad36afb9f7331ac9c3e4
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
# shellcheck disable=SC2034,SC2148,SC2128,SC2154,SC2164
|
# shellcheck disable=SC2034,SC2148,SC2128,SC2154,SC2164
|
||||||
# Maintainer: javalsai <javalsai@proton.me>
|
# Maintainer: javalsai <javalsai@proton.me>
|
||||||
pkgname=lidm-bin
|
pkgname=lidm-bin
|
||||||
pkgver=0.2.1
|
pkgver=0.2.2
|
||||||
pkgrel=2
|
pkgrel=1
|
||||||
depends=('pam')
|
depends=('pam')
|
||||||
pkgdesc="A fully colorful customizable TUI display manager made in C. (release binary)"
|
pkgdesc="A fully colorful customizable TUI display manager made in C. (release binary)"
|
||||||
arch=('x86_64')
|
arch=('x86_64')
|
||||||
@ -16,8 +16,8 @@ source=(
|
|||||||
"lidm.1::https://raw.githubusercontent.com/javalsai/lidm/v$pkgver/assets/man/lidm.1"
|
"lidm.1::https://raw.githubusercontent.com/javalsai/lidm/v$pkgver/assets/man/lidm.1"
|
||||||
"lidm-config.5::https://raw.githubusercontent.com/javalsai/lidm/v$pkgver/assets/man/lidm-config.5"
|
"lidm-config.5::https://raw.githubusercontent.com/javalsai/lidm/v$pkgver/assets/man/lidm-config.5"
|
||||||
)
|
)
|
||||||
sha256sums=('4969018d527613729336abd51e37283ce77d7c7a2233434642804b88e550e622'
|
sha256sums=('be2307be9bef7b3ef294fb0e8178040e2d8ccff6b8efb7546514da8b0f69c179'
|
||||||
'27db9b0cd2da80c0c60dcb13dfad0f9d65e7dddbb7b344b859803b9ac3943cd7'
|
'68662430a6d262b35cc54d9f0e164ed935b7f7f4497a87cc94946c558bbe8a91'
|
||||||
'a6807a55ff72ec5a5678583156b3efd0d367f0bcb79854094132771f0cb86bce'
|
'a6807a55ff72ec5a5678583156b3efd0d367f0bcb79854094132771f0cb86bce'
|
||||||
'3adaae60f79dff1cef2b2aba7dcea04196cd49816759ad36afb9f7331ac9c3e4')
|
'3adaae60f79dff1cef2b2aba7dcea04196cd49816759ad36afb9f7331ac9c3e4')
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# shellcheck disable=SC2034,SC2148,SC2128,SC2154,SC2164
|
# shellcheck disable=SC2034,SC2148,SC2128,SC2154,SC2164
|
||||||
# Maintainer: javalsai <javalsai@proton.me>
|
# Maintainer: javalsai <javalsai@proton.me>
|
||||||
pkgname=lidm-git
|
pkgname=lidm-git
|
||||||
pkgver=0.2.1.r0.ge2014f4
|
pkgver=0.2.2.r0.gb22ae6b
|
||||||
pkgrel=1
|
pkgrel=1
|
||||||
depends=('pam')
|
depends=('pam')
|
||||||
makedepends=('git' 'make' 'gcc')
|
makedepends=('git' 'make' 'gcc')
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
pkgbase = lidm
|
pkgbase = lidm
|
||||||
pkgdesc = A fully colorful customizable TUI display manager made in C. (build latest tag)
|
pkgdesc = A fully colorful customizable TUI display manager made in C. (build latest tag)
|
||||||
pkgver = 0.2.1
|
pkgver = 0.2.2
|
||||||
pkgrel = 1
|
pkgrel = 1
|
||||||
url = https://github.com/javalsai/lidm
|
url = https://github.com/javalsai/lidm
|
||||||
arch = any
|
arch = any
|
||||||
@ -8,7 +8,7 @@ pkgbase = lidm
|
|||||||
makedepends = git
|
makedepends = git
|
||||||
makedepends = gcc
|
makedepends = gcc
|
||||||
depends = pam
|
depends = pam
|
||||||
source = tarball.tar.gz::https://github.com/javalsai/lidm/archive/refs/tags/v0.2.1.tar.gz
|
source = tarball.tar.gz::https://github.com/javalsai/lidm/archive/refs/tags/v0.2.2.tar.gz
|
||||||
sha256sums = 56aaf8025fac16f5deef3058274635198fc3bf3f7eadc1de5c6539614f03d84b
|
sha256sums = 25523abc3ce6f2f261bff0cc52663607fe114692d6d3736f27fd843cae3f3a27
|
||||||
|
|
||||||
pkgname = lidm
|
pkgname = lidm
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# shellcheck disable=SC2034,SC2148,SC2128,SC2154,SC2164
|
# shellcheck disable=SC2034,SC2148,SC2128,SC2154,SC2164
|
||||||
# Maintainer: javalsai <javalsai@proton.me>
|
# Maintainer: javalsai <javalsai@proton.me>
|
||||||
pkgname=lidm
|
pkgname=lidm
|
||||||
pkgver=0.2.1
|
pkgver=0.2.2
|
||||||
pkgrel=1
|
pkgrel=1
|
||||||
depends=('pam')
|
depends=('pam')
|
||||||
makedepends=('git' 'gcc')
|
makedepends=('git' 'gcc')
|
||||||
@ -10,7 +10,7 @@ arch=('any')
|
|||||||
url="https://github.com/javalsai/lidm"
|
url="https://github.com/javalsai/lidm"
|
||||||
license=('GPL')
|
license=('GPL')
|
||||||
source=("tarball.tar.gz::https://github.com/javalsai/lidm/archive/refs/tags/v$pkgver.tar.gz")
|
source=("tarball.tar.gz::https://github.com/javalsai/lidm/archive/refs/tags/v$pkgver.tar.gz")
|
||||||
sha256sums=('56aaf8025fac16f5deef3058274635198fc3bf3f7eadc1de5c6539614f03d84b')
|
sha256sums=('25523abc3ce6f2f261bff0cc52663607fe114692d6d3736f27fd843cae3f3a27')
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
tar -xzf "tarball.tar.gz"
|
tar -xzf "tarball.tar.gz"
|
||||||
|
@ -5,8 +5,14 @@ MYSELF=$(realpath "$0")
|
|||||||
MYDIR=$(dirname "$MYSELF")
|
MYDIR=$(dirname "$MYSELF")
|
||||||
|
|
||||||
for pkg in "$MYDIR"/*/; do
|
for pkg in "$MYDIR"/*/; do
|
||||||
cd "$pkg"
|
|
||||||
printf "\x1b[1mEntering '%s'\x1b[0m\n" "$pkg"
|
printf "\x1b[1mEntering '%s'\x1b[0m\n" "$pkg"
|
||||||
|
cd "$pkg"
|
||||||
|
|
||||||
|
# shellcheck disable=SC1091
|
||||||
|
source "PKGBUILD"
|
||||||
|
for source in "${source[@]}"; do
|
||||||
|
awk -F'::' '{print $1}' <<<"$source" | xargs rm -rf
|
||||||
|
done
|
||||||
|
|
||||||
rm -rf ./*.{gz,zst} src pkg
|
rm -rf ./*.{gz,zst} src pkg
|
||||||
makepkg -f .
|
makepkg -f .
|
||||||
|
@ -15,12 +15,14 @@ void strcln(char **dest, const char *source);
|
|||||||
|
|
||||||
struct Vector {
|
struct Vector {
|
||||||
uint32_t length;
|
uint32_t length;
|
||||||
uint32_t alloc_len;
|
uint32_t capacity;
|
||||||
uint16_t alloc_size;
|
|
||||||
void **pages;
|
void **pages;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Vector vec_new();
|
struct Vector vec_new();
|
||||||
|
int vec_resize(struct Vector *, size_t size);
|
||||||
|
int vec_reserve(struct Vector *, size_t size);
|
||||||
|
int vec_reserve_exact(struct Vector *, size_t size);
|
||||||
int vec_push(struct Vector *, void *item);
|
int vec_push(struct Vector *, void *item);
|
||||||
void vec_free(struct Vector *);
|
void vec_free(struct Vector *);
|
||||||
void vec_clear(struct Vector *);
|
void vec_clear(struct Vector *);
|
||||||
|
33
src/config.c
33
src/config.c
@ -4,6 +4,14 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
// Alr so ima explain the bitfield returned by `cb` a bit
|
||||||
|
// 4 bits:
|
||||||
|
// 0b0001: break out of parsing (returning true)
|
||||||
|
// 0b0010: free the value
|
||||||
|
// 0b0100: free the key
|
||||||
|
// 0b1000: break out of parsing (returning false)
|
||||||
|
//
|
||||||
|
// This would return true if everything goes fine, false otherwise (malloc error, broke parsing, etc)
|
||||||
bool line_parser(FILE *fd, ssize_t *blksize,
|
bool line_parser(FILE *fd, ssize_t *blksize,
|
||||||
u_char (*cb)(char *key, char *value)) {
|
u_char (*cb)(char *key, char *value)) {
|
||||||
size_t opt_size = 4096;
|
size_t opt_size = 4096;
|
||||||
@ -41,19 +49,20 @@ bool line_parser(FILE *fd, ssize_t *blksize,
|
|||||||
if (ret & 0b1000) {
|
if (ret & 0b1000) {
|
||||||
free(buf);
|
free(buf);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (ret & 0b0001) {
|
if (ret & 0b0001) {
|
||||||
free(buf);
|
free(buf);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct config *__config;
|
struct config *__config;
|
||||||
|
// Yanderedev code (wanna fix this with a table or smth)
|
||||||
u_char config_line_handler(char *k, char *v) {
|
u_char config_line_handler(char *k, char *v) {
|
||||||
if (strcmp(k, "colors.bg") == 0)
|
if (strcmp(k, "colors.bg") == 0)
|
||||||
__config->theme.colors.bg = v;
|
__config->theme.colors.bg = v;
|
||||||
@ -98,16 +107,13 @@ u_char config_line_handler(char *k, char *v) {
|
|||||||
else if (strcmp(k, "functions.poweroff") == 0) {
|
else if (strcmp(k, "functions.poweroff") == 0) {
|
||||||
__config->functions.poweroff = find_keyname(v);
|
__config->functions.poweroff = find_keyname(v);
|
||||||
return 0b0110;
|
return 0b0110;
|
||||||
}
|
} else if (strcmp(k, "functions.reboot") == 0) {
|
||||||
else if (strcmp(k, "functions.reboot") == 0) {
|
|
||||||
__config->functions.reboot = find_keyname(v);
|
__config->functions.reboot = find_keyname(v);
|
||||||
return 0b0110;
|
return 0b0110;
|
||||||
}
|
} else if (strcmp(k, "functions.refresh") == 0) {
|
||||||
else if (strcmp(k, "functions.refresh") == 0) {
|
|
||||||
__config->functions.refresh = find_keyname(v);
|
__config->functions.refresh = find_keyname(v);
|
||||||
return 0b0110;
|
return 0b0110;
|
||||||
}
|
} else if (strcmp(k, "strings.f_poweroff") == 0)
|
||||||
else if (strcmp(k, "strings.f_poweroff") == 0)
|
|
||||||
__config->strings.f_poweroff = v;
|
__config->strings.f_poweroff = v;
|
||||||
else if (strcmp(k, "strings.f_reboot") == 0)
|
else if (strcmp(k, "strings.f_reboot") == 0)
|
||||||
__config->strings.f_reboot = v;
|
__config->strings.f_reboot = v;
|
||||||
@ -126,15 +132,14 @@ u_char config_line_handler(char *k, char *v) {
|
|||||||
else if (strcmp(k, "behavior.include_defshell") == 0) {
|
else if (strcmp(k, "behavior.include_defshell") == 0) {
|
||||||
__config->behavior.include_defshell = strcmp(v, "true") == 0;
|
__config->behavior.include_defshell = strcmp(v, "true") == 0;
|
||||||
return 0b0110;
|
return 0b0110;
|
||||||
}
|
} else if (strcmp(k, "behavior.source") == 0)
|
||||||
else if (strcmp(k, "behavior.source") == 0)
|
|
||||||
vec_push(&__config->behavior.source, v);
|
vec_push(&__config->behavior.source, v);
|
||||||
else if (strcmp(k, "behavior.user_source") == 0)
|
else if (strcmp(k, "behavior.user_source") == 0)
|
||||||
vec_push(&__config->behavior.user_source, v);
|
vec_push(&__config->behavior.user_source, v);
|
||||||
else
|
else
|
||||||
return 0b1111;
|
return 0b1111;
|
||||||
|
|
||||||
return 0b100;
|
return 0b0100;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct config *parse_config(char *path) {
|
struct config *parse_config(char *path) {
|
||||||
|
@ -54,6 +54,7 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
|
|||||||
char *exec_buf = NULL;
|
char *exec_buf = NULL;
|
||||||
char *tryexec_buf = NULL;
|
char *tryexec_buf = NULL;
|
||||||
// This should be made a specific function
|
// This should be made a specific function
|
||||||
|
// Emm, if anything goes wrong just free the inner loop and `break;` fd and the rest is handled after
|
||||||
while (true) {
|
while (true) {
|
||||||
char *buf = malloc(sb->st_blksize);
|
char *buf = malloc(sb->st_blksize);
|
||||||
ssize_t read_size = getline(&buf, &alloc_size, fd);
|
ssize_t read_size = getline(&buf, &alloc_size, fd);
|
||||||
@ -64,7 +65,19 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
|
|||||||
|
|
||||||
uint read;
|
uint read;
|
||||||
char *key = malloc(read_size + sizeof(char));
|
char *key = malloc(read_size + sizeof(char));
|
||||||
|
if(key == NULL) {
|
||||||
|
free(buf);
|
||||||
|
// TODO: more sophisticated error handling??
|
||||||
|
break;
|
||||||
|
}
|
||||||
char *value = malloc(read_size + sizeof(char));
|
char *value = malloc(read_size + sizeof(char));
|
||||||
|
if(value == NULL) {
|
||||||
|
free(buf);
|
||||||
|
free(key);
|
||||||
|
// TODO: more sophisticated error handling??
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
value[0] = '\0'; // I'm not sure if sscanf would null this string out
|
||||||
if ((read = sscanf(buf, "%[^=]=%[^\n]\n", key, value)) != 0) {
|
if ((read = sscanf(buf, "%[^=]=%[^\n]\n", key, value)) != 0) {
|
||||||
if (strcmp(key, "Name") == 0) {
|
if (strcmp(key, "Name") == 0) {
|
||||||
found &= 0b001;
|
found &= 0b001;
|
||||||
@ -78,13 +91,17 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
|
|||||||
} else {
|
} else {
|
||||||
free(value);
|
free(value);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
free(value);
|
||||||
|
}
|
||||||
free(key);
|
free(key);
|
||||||
free(buf);
|
free(buf);
|
||||||
if (found == 0b111) break;
|
if (found == 0b111) break;
|
||||||
}
|
}
|
||||||
/*printf("\nend parsing...\n");*/
|
/*printf("\nend parsing...\n");*/
|
||||||
|
|
||||||
|
// Generic handling of exit
|
||||||
|
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
|
||||||
// just add this to the list
|
// just add this to the list
|
||||||
@ -108,6 +125,7 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
|
|||||||
// This code is designed to be run purely single threaded
|
// This code is designed to be run purely single threaded
|
||||||
struct Vector get_avaliable_sessions() {
|
struct Vector get_avaliable_sessions() {
|
||||||
struct Vector sessions = vec_new();
|
struct Vector sessions = vec_new();
|
||||||
|
vec_reserve(&sessions, 8);
|
||||||
|
|
||||||
cb_sessions = &sessions;
|
cb_sessions = &sessions;
|
||||||
for (size_t i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) {
|
for (size_t i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) {
|
||||||
|
7
src/ui.c
7
src/ui.c
@ -322,6 +322,7 @@ void ffield_type(char *text) {
|
|||||||
print_ffield();
|
print_ffield();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char* unknown_str = "unknown";
|
||||||
int load(struct Vector *users, struct Vector *sessions) {
|
int load(struct Vector *users, struct Vector *sessions) {
|
||||||
/// SETUP
|
/// SETUP
|
||||||
gusers = users;
|
gusers = users;
|
||||||
@ -331,9 +332,9 @@ int load(struct Vector *users, struct Vector *sessions) {
|
|||||||
char *hostname = malloc(16);
|
char *hostname = malloc(16);
|
||||||
if (gethostname(hostname, 16) != 0) {
|
if (gethostname(hostname, 16) != 0) {
|
||||||
free(hostname);
|
free(hostname);
|
||||||
hostname = "unknown";
|
hostname = unknown_str;
|
||||||
} else {
|
} else {
|
||||||
hostname = realloc(hostname, strlen(hostname) + 1);
|
hostname[15] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
of_session = ofield_new(sessions->length + behavior.include_defshell);
|
of_session = ofield_new(sessions->length + behavior.include_defshell);
|
||||||
@ -350,12 +351,14 @@ int load(struct Vector *users, struct Vector *sessions) {
|
|||||||
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2,
|
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2,
|
||||||
boxstart.x + 12 - (uint)strlen(hostname), theme.colors.e_hostname,
|
boxstart.x + 12 - (uint)strlen(hostname), theme.colors.e_hostname,
|
||||||
hostname, theme.colors.fg);
|
hostname, theme.colors.fg);
|
||||||
|
if(hostname != unknown_str) free(hostname);
|
||||||
|
|
||||||
// put date
|
// put date
|
||||||
char *fmtd_time = fmt_time();
|
char *fmtd_time = fmt_time();
|
||||||
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2,
|
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2,
|
||||||
boxstart.x + boxw - 3 - (uint)strlen(fmtd_time), theme.colors.e_date,
|
boxstart.x + boxw - 3 - (uint)strlen(fmtd_time), theme.colors.e_date,
|
||||||
fmtd_time, theme.colors.fg);
|
fmtd_time, theme.colors.fg);
|
||||||
|
free(fmtd_time);
|
||||||
|
|
||||||
print_field(SESSION);
|
print_field(SESSION);
|
||||||
print_field(USER);
|
print_field(USER);
|
||||||
|
@ -23,6 +23,7 @@ static struct user __new_user(struct passwd *p) {
|
|||||||
// This code is designed to be run purely single threaded
|
// This code is designed to be run purely single threaded
|
||||||
struct Vector get_human_users() {
|
struct Vector get_human_users() {
|
||||||
struct Vector users = vec_new();
|
struct Vector users = vec_new();
|
||||||
|
vec_reserve(&users, 4);
|
||||||
|
|
||||||
struct passwd *pwd;
|
struct passwd *pwd;
|
||||||
while ((pwd = getpwent()) != NULL) {
|
while ((pwd = getpwent()) != NULL) {
|
||||||
|
48
src/util.c
48
src/util.c
@ -75,20 +75,41 @@ struct Vector vec_new() {
|
|||||||
return vec;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
int vec_push(struct Vector *vec, void *item) {
|
int vec_resize(struct Vector *vec, size_t size) {
|
||||||
if (vec->length >= vec->alloc_len) {
|
void **new_location = realloc(vec->pages, size * sizeof(void*));
|
||||||
uint32_t new_size = vec->alloc_len + vec->alloc_size;
|
if (new_location != NULL) {
|
||||||
void **new_location = realloc(vec->pages, vec->alloc_size);
|
if (vec->length > size)
|
||||||
if (new_location != NULL) {
|
vec->length = size;
|
||||||
vec->alloc_size = new_size;
|
vec->capacity = size;
|
||||||
vec->pages = new_location;
|
vec->pages = new_location;
|
||||||
} else {
|
} else {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
vec->pages[vec->length] = item;
|
int vec_reserve(struct Vector *vec, size_t size) {
|
||||||
vec->length++;
|
uint32_t new_capacity = vec->capacity;
|
||||||
|
while (vec->length + size > new_capacity) {
|
||||||
|
new_capacity = new_capacity + (new_capacity >> 1) + 1; // cap * 1.5 + 1; 0 1 2 4 7 11...
|
||||||
|
}
|
||||||
|
return vec_resize(vec, new_capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
int vec_reserve_exact(struct Vector *vec, size_t size) {
|
||||||
|
uint32_t needed_capacity = vec->length + size;
|
||||||
|
if (vec->capacity < needed_capacity) {
|
||||||
|
return vec_resize(vec, needed_capacity);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int vec_push(struct Vector *vec, void *item) {
|
||||||
|
int res_ret = vec_reserve(vec, 1);
|
||||||
|
if(res_ret != 0) return res_ret;
|
||||||
|
|
||||||
|
vec->pages[vec->length++] = item;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,8 +127,7 @@ void vec_clear(struct Vector *vec) {
|
|||||||
|
|
||||||
void vec_reset(struct Vector *vec) {
|
void vec_reset(struct Vector *vec) {
|
||||||
vec->length = 0;
|
vec->length = 0;
|
||||||
vec->alloc_len = 0;
|
vec->capacity = 0;
|
||||||
vec->alloc_size = 4096; // 4KiB page size?
|
|
||||||
vec->pages = NULL;
|
vec->pages = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user