forked from vitrine/wmaker
Compare commits
1 Commits
refactor/w
...
malloc-rs
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d91275d959 |
@@ -39,7 +39,7 @@ ACLOCAL_AMFLAGS = -I m4
|
||||
AM_DISTCHECK_CONFIGURE_FLAGS = --enable-silent-rules LINGUAS='*'
|
||||
|
||||
|
||||
SUBDIRS = wrlib wutil-rs WINGs wmaker-rs src util po WindowMaker wmlib WPrefs.app doc
|
||||
SUBDIRS = wrlib WINGs wmaker-rs src util po WindowMaker wmlib WPrefs.app doc
|
||||
DIST_SUBDIRS = $(SUBDIRS) test
|
||||
|
||||
EXTRA_DIST = TODO BUGS BUGFORM FAQ INSTALL \
|
||||
|
||||
@@ -10,17 +10,14 @@ libWUtil_la_LDFLAGS = -version-info @WUTIL_VERSION@
|
||||
|
||||
lib_LTLIBRARIES = libWUtil.la libWINGs.la
|
||||
|
||||
wutilrs = $(top_builddir)/wutil-rs/target/debug/libwutil_rs.a
|
||||
wraster = $(top_builddir)/wrlib/libwraster.la
|
||||
|
||||
LDADD= libWUtil.la libWINGs.la $(wraster) $(wutilrs) @INTLIBS@
|
||||
libWINGs_la_LIBADD = libWUtil.la $(wraster) $(wutilrs) @XLIBS@ @XFT_LIBS@ @FCLIBS@ @LIBM@ @PANGO_LIBS@
|
||||
libWUtil_la_LIBADD = @LIBBSD@ $(wutilrs)
|
||||
LDADD= libWUtil.la libWINGs.la $(top_builddir)/wrlib/libwraster.la @INTLIBS@
|
||||
libWINGs_la_LIBADD = libWUtil.la $(top_builddir)/wrlib/libwraster.la @XLIBS@ @XFT_LIBS@ @FCLIBS@ @LIBM@ @PANGO_LIBS@
|
||||
libWUtil_la_LIBADD = @LIBBSD@
|
||||
|
||||
EXTRA_DIST = BUGS make-rgb Examples Extras Tests
|
||||
|
||||
|
||||
|
||||
# wbutton.c
|
||||
libWINGs_la_SOURCES = \
|
||||
configuration.c \
|
||||
@@ -72,6 +69,8 @@ libWUtil_la_SOURCES = \
|
||||
error.h \
|
||||
findfile.c \
|
||||
handlers.c \
|
||||
hashtable.c \
|
||||
memory.c \
|
||||
menuparser.c \
|
||||
menuparser.h \
|
||||
menuparser_macros.c \
|
||||
|
||||
@@ -169,6 +169,10 @@ typedef struct {
|
||||
unsigned (*hash)(const void *);
|
||||
/* NULL is pointer compare */
|
||||
Bool (*keyIsEqual)(const void *, const void *);
|
||||
/* NULL does nothing */
|
||||
void* (*retainKey)(const void *);
|
||||
/* NULL does nothing */
|
||||
void (*releaseKey)(const void *);
|
||||
} WMHashTableCallbacks;
|
||||
|
||||
|
||||
@@ -209,6 +213,10 @@ void wfree(void *ptr);
|
||||
void wrelease(void *ptr);
|
||||
void* wretain(void *ptr);
|
||||
|
||||
typedef void waborthandler(int);
|
||||
|
||||
waborthandler* wsetabort(waborthandler* handler);
|
||||
|
||||
/* ---[ WINGs/error.c ]--------------------------------------------------- */
|
||||
|
||||
enum {
|
||||
@@ -334,8 +342,7 @@ void WHandleEvents(void);
|
||||
/* ---[ WINGs/hashtable.c ]----------------------------------------------- */
|
||||
|
||||
|
||||
WMHashTable* WMCreateIdentityHashTable();
|
||||
WMHashTable* WMCreateStringHashTable();
|
||||
WMHashTable* WMCreateHashTable(const WMHashTableCallbacks callbacks);
|
||||
|
||||
void WMFreeHashTable(WMHashTable *table);
|
||||
|
||||
@@ -385,6 +392,10 @@ Bool WMNextHashEnumeratorItemAndKey(WMHashEnumerator *enumerator,
|
||||
extern const WMHashTableCallbacks WMIntHashCallbacks;
|
||||
/* sizeof(keys) are <= sizeof(void*) */
|
||||
|
||||
extern const WMHashTableCallbacks WMStringHashCallbacks;
|
||||
/* keys are strings. Strings will be copied with wstrdup()
|
||||
* and freed with wfree() */
|
||||
|
||||
extern const WMHashTableCallbacks WMStringPointerHashCallbacks;
|
||||
/* keys are strings, but they are not copied */
|
||||
|
||||
|
||||
@@ -456,7 +456,7 @@ int wcopy_file(const char *dest_dir, const char *src_file, const char *dest_file
|
||||
return -1;
|
||||
}
|
||||
|
||||
buffer = malloc(buffer_size); /* Don't use wmalloc to avoid the memset(0) we don't need */
|
||||
buffer = wmalloc(buffer_size);
|
||||
if (buffer == NULL) {
|
||||
werror(_("could not allocate memory for the copy buffer"));
|
||||
close(fd_dst);
|
||||
@@ -508,14 +508,14 @@ int wcopy_file(const char *dest_dir, const char *src_file, const char *dest_file
|
||||
if (close(fd_dst) != 0) {
|
||||
werror(_("could not close the file \"%s\": %s"), path_dst, strerror(errno));
|
||||
cleanup_and_return_failure:
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
close(fd_src);
|
||||
unlink(path_dst);
|
||||
wfree(path_dst);
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
wfree(path_dst);
|
||||
close(fd_src);
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ WMHandlerID WMAddTimerHandler(int milliseconds, WMCallback * callback, void *cda
|
||||
{
|
||||
TimerHandler *handler;
|
||||
|
||||
handler = malloc(sizeof(TimerHandler));
|
||||
handler = wmalloc(sizeof(TimerHandler));
|
||||
if (!handler)
|
||||
return NULL;
|
||||
|
||||
@@ -214,7 +214,7 @@ WMHandlerID WMAddIdleHandler(WMCallback * callback, void *cdata)
|
||||
{
|
||||
IdleHandler *handler;
|
||||
|
||||
handler = malloc(sizeof(IdleHandler));
|
||||
handler = wmalloc(sizeof(IdleHandler));
|
||||
if (!handler)
|
||||
return NULL;
|
||||
|
||||
|
||||
422
WINGs/hashtable.c
Normal file
422
WINGs/hashtable.c
Normal file
@@ -0,0 +1,422 @@
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "WUtil.h"
|
||||
|
||||
#define INITIAL_CAPACITY 23
|
||||
|
||||
|
||||
typedef struct HashItem {
|
||||
const void *key;
|
||||
const void *data;
|
||||
|
||||
struct HashItem *next; /* collided item list */
|
||||
} HashItem;
|
||||
|
||||
typedef struct W_HashTable {
|
||||
WMHashTableCallbacks callbacks;
|
||||
|
||||
unsigned itemCount;
|
||||
unsigned size; /* table size */
|
||||
|
||||
HashItem **table;
|
||||
} HashTable;
|
||||
|
||||
#define HASH(table, key) (((table)->callbacks.hash ? \
|
||||
(*(table)->callbacks.hash)(key) : hashPtr(key)) % (table)->size)
|
||||
|
||||
#define DUPKEY(table, key) ((table)->callbacks.retainKey ? \
|
||||
(*(table)->callbacks.retainKey)(key) : (key))
|
||||
|
||||
#define RELKEY(table, key) if ((table)->callbacks.releaseKey) \
|
||||
(*(table)->callbacks.releaseKey)(key)
|
||||
|
||||
static inline unsigned hashString(const void *param)
|
||||
{
|
||||
const char *key = param;
|
||||
unsigned ret = 0;
|
||||
unsigned ctr = 0;
|
||||
|
||||
while (*key) {
|
||||
ret ^= *key++ << ctr;
|
||||
ctr = (ctr + 1) % sizeof(char *);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline unsigned hashPtr(const void *key)
|
||||
{
|
||||
return ((size_t) key / sizeof(char *));
|
||||
}
|
||||
|
||||
static void rellocateItem(WMHashTable * table, HashItem * item)
|
||||
{
|
||||
unsigned h;
|
||||
|
||||
h = HASH(table, item->key);
|
||||
|
||||
item->next = table->table[h];
|
||||
table->table[h] = item;
|
||||
}
|
||||
|
||||
static void rebuildTable(WMHashTable * table)
|
||||
{
|
||||
HashItem *next;
|
||||
HashItem **oldArray;
|
||||
int i;
|
||||
int oldSize;
|
||||
int newSize;
|
||||
|
||||
oldArray = table->table;
|
||||
oldSize = table->size;
|
||||
|
||||
newSize = table->size * 2;
|
||||
|
||||
table->table = wmalloc(sizeof(char *) * newSize);
|
||||
table->size = newSize;
|
||||
|
||||
for (i = 0; i < oldSize; i++) {
|
||||
while (oldArray[i] != NULL) {
|
||||
next = oldArray[i]->next;
|
||||
rellocateItem(table, oldArray[i]);
|
||||
oldArray[i] = next;
|
||||
}
|
||||
}
|
||||
wfree(oldArray);
|
||||
}
|
||||
|
||||
WMHashTable *WMCreateHashTable(const WMHashTableCallbacks callbacks)
|
||||
{
|
||||
HashTable *table;
|
||||
|
||||
table = wmalloc(sizeof(HashTable));
|
||||
|
||||
table->callbacks = callbacks;
|
||||
|
||||
table->size = INITIAL_CAPACITY;
|
||||
|
||||
table->table = wmalloc(sizeof(HashItem *) * table->size);
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
void WMResetHashTable(WMHashTable * table)
|
||||
{
|
||||
HashItem *item, *tmp;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < table->size; i++) {
|
||||
item = table->table[i];
|
||||
while (item) {
|
||||
tmp = item->next;
|
||||
RELKEY(table, item->key);
|
||||
wfree(item);
|
||||
item = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
table->itemCount = 0;
|
||||
|
||||
if (table->size > INITIAL_CAPACITY) {
|
||||
wfree(table->table);
|
||||
table->size = INITIAL_CAPACITY;
|
||||
table->table = wmalloc(sizeof(HashItem *) * table->size);
|
||||
} else {
|
||||
memset(table->table, 0, sizeof(HashItem *) * table->size);
|
||||
}
|
||||
}
|
||||
|
||||
void WMFreeHashTable(WMHashTable * table)
|
||||
{
|
||||
HashItem *item, *tmp;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < table->size; i++) {
|
||||
item = table->table[i];
|
||||
while (item) {
|
||||
tmp = item->next;
|
||||
RELKEY(table, item->key);
|
||||
wfree(item);
|
||||
item = tmp;
|
||||
}
|
||||
}
|
||||
wfree(table->table);
|
||||
wfree(table);
|
||||
}
|
||||
|
||||
unsigned WMCountHashTable(WMHashTable * table)
|
||||
{
|
||||
return table->itemCount;
|
||||
}
|
||||
|
||||
static HashItem *hashGetItem(WMHashTable *table, const void *key)
|
||||
{
|
||||
unsigned h;
|
||||
HashItem *item;
|
||||
|
||||
h = HASH(table, key);
|
||||
item = table->table[h];
|
||||
|
||||
if (table->callbacks.keyIsEqual) {
|
||||
while (item) {
|
||||
if ((*table->callbacks.keyIsEqual) (key, item->key)) {
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
} else {
|
||||
while (item) {
|
||||
if (key == item->key) {
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
void *WMHashGet(WMHashTable * table, const void *key)
|
||||
{
|
||||
HashItem *item;
|
||||
|
||||
item = hashGetItem(table, key);
|
||||
if (!item)
|
||||
return NULL;
|
||||
return (void *)item->data;
|
||||
}
|
||||
|
||||
Bool WMHashGetItemAndKey(WMHashTable * table, const void *key, void **retItem, void **retKey)
|
||||
{
|
||||
HashItem *item;
|
||||
|
||||
item = hashGetItem(table, key);
|
||||
if (!item)
|
||||
return False;
|
||||
|
||||
if (retKey)
|
||||
*retKey = (void *)item->key;
|
||||
if (retItem)
|
||||
*retItem = (void *)item->data;
|
||||
return True;
|
||||
}
|
||||
|
||||
void *WMHashInsert(WMHashTable * table, const void *key, const void *data)
|
||||
{
|
||||
unsigned h;
|
||||
HashItem *item;
|
||||
int replacing = 0;
|
||||
|
||||
h = HASH(table, key);
|
||||
/* look for the entry */
|
||||
item = table->table[h];
|
||||
if (table->callbacks.keyIsEqual) {
|
||||
while (item) {
|
||||
if ((*table->callbacks.keyIsEqual) (key, item->key)) {
|
||||
replacing = 1;
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
} else {
|
||||
while (item) {
|
||||
if (key == item->key) {
|
||||
replacing = 1;
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (replacing) {
|
||||
const void *old;
|
||||
|
||||
old = item->data;
|
||||
item->data = data;
|
||||
RELKEY(table, item->key);
|
||||
item->key = DUPKEY(table, key);
|
||||
|
||||
return (void *)old;
|
||||
} else {
|
||||
HashItem *nitem;
|
||||
|
||||
nitem = wmalloc(sizeof(HashItem));
|
||||
nitem->key = DUPKEY(table, key);
|
||||
nitem->data = data;
|
||||
nitem->next = table->table[h];
|
||||
table->table[h] = nitem;
|
||||
|
||||
table->itemCount++;
|
||||
}
|
||||
|
||||
/* OPTIMIZE: put this in an idle handler. */
|
||||
if (table->itemCount > table->size) {
|
||||
#ifdef DEBUG0
|
||||
printf("rebuilding hash table...\n");
|
||||
#endif
|
||||
rebuildTable(table);
|
||||
#ifdef DEBUG0
|
||||
printf("finished rebuild.\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static HashItem *deleteFromList(HashTable * table, HashItem * item, const void *key)
|
||||
{
|
||||
HashItem *next;
|
||||
|
||||
if (item == NULL)
|
||||
return NULL;
|
||||
|
||||
if ((table->callbacks.keyIsEqual && (*table->callbacks.keyIsEqual) (key, item->key))
|
||||
|| (!table->callbacks.keyIsEqual && key == item->key)) {
|
||||
|
||||
next = item->next;
|
||||
RELKEY(table, item->key);
|
||||
wfree(item);
|
||||
|
||||
table->itemCount--;
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
item->next = deleteFromList(table, item->next, key);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
void WMHashRemove(WMHashTable * table, const void *key)
|
||||
{
|
||||
unsigned h;
|
||||
|
||||
h = HASH(table, key);
|
||||
|
||||
table->table[h] = deleteFromList(table, table->table[h], key);
|
||||
}
|
||||
|
||||
WMHashEnumerator WMEnumerateHashTable(WMHashTable * table)
|
||||
{
|
||||
WMHashEnumerator enumerator;
|
||||
|
||||
enumerator.table = table;
|
||||
enumerator.index = 0;
|
||||
enumerator.nextItem = table->table[0];
|
||||
|
||||
return enumerator;
|
||||
}
|
||||
|
||||
void *WMNextHashEnumeratorItem(WMHashEnumerator * enumerator)
|
||||
{
|
||||
const void *data = NULL;
|
||||
|
||||
/* this assumes the table doesn't change between
|
||||
* WMEnumerateHashTable() and WMNextHashEnumeratorItem() calls */
|
||||
|
||||
if (enumerator->nextItem == NULL) {
|
||||
HashTable *table = enumerator->table;
|
||||
while (++enumerator->index < table->size) {
|
||||
if (table->table[enumerator->index] != NULL) {
|
||||
enumerator->nextItem = table->table[enumerator->index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (enumerator->nextItem) {
|
||||
data = ((HashItem *) enumerator->nextItem)->data;
|
||||
enumerator->nextItem = ((HashItem *) enumerator->nextItem)->next;
|
||||
}
|
||||
|
||||
return (void *)data;
|
||||
}
|
||||
|
||||
void *WMNextHashEnumeratorKey(WMHashEnumerator * enumerator)
|
||||
{
|
||||
const void *key = NULL;
|
||||
|
||||
/* this assumes the table doesn't change between
|
||||
* WMEnumerateHashTable() and WMNextHashEnumeratorKey() calls */
|
||||
|
||||
if (enumerator->nextItem == NULL) {
|
||||
HashTable *table = enumerator->table;
|
||||
while (++enumerator->index < table->size) {
|
||||
if (table->table[enumerator->index] != NULL) {
|
||||
enumerator->nextItem = table->table[enumerator->index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (enumerator->nextItem) {
|
||||
key = ((HashItem *) enumerator->nextItem)->key;
|
||||
enumerator->nextItem = ((HashItem *) enumerator->nextItem)->next;
|
||||
}
|
||||
|
||||
return (void *)key;
|
||||
}
|
||||
|
||||
Bool WMNextHashEnumeratorItemAndKey(WMHashEnumerator * enumerator, void **item, void **key)
|
||||
{
|
||||
/* this assumes the table doesn't change between
|
||||
* WMEnumerateHashTable() and WMNextHashEnumeratorItemAndKey() calls */
|
||||
|
||||
if (enumerator->nextItem == NULL) {
|
||||
HashTable *table = enumerator->table;
|
||||
while (++enumerator->index < table->size) {
|
||||
if (table->table[enumerator->index] != NULL) {
|
||||
enumerator->nextItem = table->table[enumerator->index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (enumerator->nextItem) {
|
||||
if (item)
|
||||
*item = (void *)((HashItem *) enumerator->nextItem)->data;
|
||||
if (key)
|
||||
*key = (void *)((HashItem *) enumerator->nextItem)->key;
|
||||
enumerator->nextItem = ((HashItem *) enumerator->nextItem)->next;
|
||||
|
||||
return True;
|
||||
}
|
||||
|
||||
return False;
|
||||
}
|
||||
|
||||
static Bool compareStrings(const void *param1, const void *param2)
|
||||
{
|
||||
const char *key1 = param1;
|
||||
const char *key2 = param2;
|
||||
|
||||
return strcmp(key1, key2) == 0;
|
||||
}
|
||||
|
||||
typedef void *(*retainFunc) (const void *);
|
||||
typedef void (*releaseFunc) (const void *);
|
||||
|
||||
const WMHashTableCallbacks WMIntHashCallbacks = {
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
const WMHashTableCallbacks WMStringHashCallbacks = {
|
||||
hashString,
|
||||
compareStrings,
|
||||
(retainFunc) wstrdup,
|
||||
(releaseFunc) wfree
|
||||
};
|
||||
|
||||
const WMHashTableCallbacks WMStringPointerHashCallbacks = {
|
||||
hashString,
|
||||
compareStrings,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
55
WINGs/memory.c
Normal file
55
WINGs/memory.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Window Maker miscelaneous function library
|
||||
*
|
||||
* Copyright (c) 1997-2003 Alfredo K. Kojima
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "WUtil.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdnoreturn.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void defaultHandler(int bla)
|
||||
{
|
||||
if (bla)
|
||||
kill(getpid(), SIGABRT);
|
||||
else
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static waborthandler *aborthandler = defaultHandler;
|
||||
|
||||
static inline noreturn void wAbort(int bla)
|
||||
{
|
||||
(*aborthandler)(bla);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
waborthandler *wsetabort(waborthandler * handler)
|
||||
{
|
||||
waborthandler *old = aborthandler;
|
||||
|
||||
aborthandler = handler;
|
||||
|
||||
return old;
|
||||
}
|
||||
@@ -88,10 +88,10 @@ static NotificationCenter *notificationCenter = NULL;
|
||||
void W_InitNotificationCenter(void)
|
||||
{
|
||||
notificationCenter = wmalloc(sizeof(NotificationCenter));
|
||||
notificationCenter->nameTable = WMCreateStringHashTable();
|
||||
notificationCenter->objectTable = WMCreateIdentityHashTable();
|
||||
notificationCenter->nameTable = WMCreateHashTable(WMStringPointerHashCallbacks);
|
||||
notificationCenter->objectTable = WMCreateHashTable(WMIntHashCallbacks);
|
||||
notificationCenter->nilList = NULL;
|
||||
notificationCenter->observerTable = WMCreateIdentityHashTable();
|
||||
notificationCenter->observerTable = WMCreateHashTable(WMIntHashCallbacks);
|
||||
}
|
||||
|
||||
void W_ReleaseNotificationCenter(void)
|
||||
|
||||
@@ -60,6 +60,8 @@ typedef Bool(*isEqualFunc) (const void *, const void *);
|
||||
static const WMHashTableCallbacks WMPropListHashCallbacks = {
|
||||
hashPropList,
|
||||
(isEqualFunc) WMIsPropListEqualTo,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
static Bool caseSensitive = True;
|
||||
@@ -988,7 +990,7 @@ WMPropList *WMCreatePLDictionary(WMPropList * key, WMPropList * value, ...)
|
||||
|
||||
plist = (WMPropList *) wmalloc(sizeof(W_PropList));
|
||||
plist->type = WPLDictionary;
|
||||
plist->d.dict = WMCreatePropListHashTable();
|
||||
plist->d.dict = WMCreateHashTable(WMPropListHashCallbacks);
|
||||
plist->retainCount = 1;
|
||||
|
||||
if (!key || !value)
|
||||
|
||||
@@ -65,7 +65,7 @@ struct W_Balloon *W_CreateBalloon(WMScreen * scr)
|
||||
W_ResizeView(bPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
||||
bPtr->flags.alignment = DEFAULT_ALIGNMENT;
|
||||
|
||||
bPtr->table = WMCreateIdentityHashTable();
|
||||
bPtr->table = WMCreateHashTable(WMIntHashCallbacks);
|
||||
|
||||
bPtr->delay = DEFAULT_DELAY;
|
||||
|
||||
|
||||
@@ -535,7 +535,7 @@ static void listFamilies(WMScreen * scr, WMFontPanel * panel)
|
||||
if (pat)
|
||||
FcPatternDestroy(pat);
|
||||
|
||||
families = WMCreateStringHashTable();
|
||||
families = WMCreateHashTable(WMStringPointerHashCallbacks);
|
||||
|
||||
if (fs) {
|
||||
for (i = 0; i < fs->nfont; i++) {
|
||||
|
||||
@@ -610,10 +610,9 @@ WMScreen *WMCreateScreenWithRContext(Display * display, int screen, RContext * c
|
||||
assert(W_ApplicationInitialized());
|
||||
}
|
||||
|
||||
scrPtr = malloc(sizeof(W_Screen));
|
||||
scrPtr = wmalloc(sizeof(W_Screen));
|
||||
if (!scrPtr)
|
||||
return NULL;
|
||||
memset(scrPtr, 0, sizeof(W_Screen));
|
||||
|
||||
scrPtr->aflags.hasAppIcon = 1;
|
||||
|
||||
@@ -630,7 +629,7 @@ WMScreen *WMCreateScreenWithRContext(Display * display, int screen, RContext * c
|
||||
|
||||
scrPtr->rootWin = RootWindow(display, screen);
|
||||
|
||||
scrPtr->fontCache = WMCreateStringHashTable();
|
||||
scrPtr->fontCache = WMCreateHashTable(WMStringPointerHashCallbacks);
|
||||
|
||||
scrPtr->xftdraw = XftDrawCreate(scrPtr->display, W_DRAWABLE(scrPtr), scrPtr->visual, scrPtr->colormap);
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ static void autoDelayChanged(void *observerData, WMNotification *notification)
|
||||
}
|
||||
char *value = WMGetTextFieldText(anAutoDelayT);
|
||||
adjustButtonSelectionBasedOnValue(panel, row, value);
|
||||
free(value);
|
||||
wfree(value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ static void storeData(_Panel * panel)
|
||||
if (sscanf(str, "%i", &i) != 1)
|
||||
i = 0;
|
||||
SetIntegerForKey(i, "RaiseDelay");
|
||||
free(str);
|
||||
wfree(str);
|
||||
|
||||
SetBoolForKey(WMGetButtonSelected(panel->ignB), "IgnoreFocusClick");
|
||||
SetBoolForKey(WMGetButtonSelected(panel->newB), "AutoFocus");
|
||||
|
||||
@@ -147,7 +147,7 @@ static void storeData(_Panel * panel)
|
||||
if (sscanf(str, "%i", &i) != 1)
|
||||
i = 0;
|
||||
SetIntegerForKey(i, "HotCornerDelay");
|
||||
free(str);
|
||||
wfree(str);
|
||||
|
||||
SetIntegerForKey(WMGetSliderValue(panel->hceS), "HotCornerEdge");
|
||||
|
||||
|
||||
@@ -66,10 +66,9 @@ AM_CPPFLAGS = -DRESOURCE_PATH=\"$(wpdatadir)\" -DWMAKER_RESOURCE_PATH=\"$(pkgdat
|
||||
WPrefs_DEPENDENCIES = $(top_builddir)/WINGs/libWINGs.la
|
||||
|
||||
WPrefs_LDADD = \
|
||||
$(top_builddir)/wutil-rs/target/debug/libwutil_rs.a\
|
||||
$(top_builddir)/wings-rs/target/debug/libwings_rs.la\
|
||||
$(top_builddir)/WINGs/libWINGs.la\
|
||||
$(top_builddir)/WINGs/libWUtil.la\
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
$(top_builddir)/wrlib/libwraster.la \
|
||||
@XLFLAGS@ @XLIBS@ \
|
||||
@LIBM@ \
|
||||
|
||||
@@ -47,6 +47,14 @@ struct {
|
||||
static pid_t DeadChildren[MAX_DEATHS];
|
||||
static int DeadChildrenCount = 0;
|
||||
|
||||
static noreturn void wAbort(Bool foo)
|
||||
{
|
||||
/* Parameter not used, but tell the compiler that it is ok */
|
||||
(void) foo;
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void print_help(const char *progname)
|
||||
{
|
||||
printf(_("usage: %s [options]\n"), progname);
|
||||
@@ -79,6 +87,8 @@ int main(int argc, char **argv)
|
||||
int i;
|
||||
char *display_name = "";
|
||||
|
||||
wsetabort(wAbort);
|
||||
|
||||
memset(DeadHandlers, 0, sizeof(DeadHandlers));
|
||||
|
||||
WMInitializeApplication("WPrefs", &argc, argv);
|
||||
|
||||
21
configure.ac
21
configure.ac
@@ -351,6 +351,24 @@ AS_IF([test "x$enable_mwm_hints" = "xno"],
|
||||
AM_CONDITIONAL([USE_MWM_HINTS], [test "x$enable_mwm_hints" != "xno"])
|
||||
|
||||
|
||||
dnl Boehm GC
|
||||
dnl ========
|
||||
m4_divert_push([INIT_PREPARE])dnl
|
||||
AC_ARG_ENABLE([boehm-gc],
|
||||
[AS_HELP_STRING([--enable-boehm-gc], [use Boehm GC instead of the default libc malloc() [default=no]])],
|
||||
[AS_CASE(["$enableval"],
|
||||
[yes], [with_boehm_gc=yes],
|
||||
[no], [with_boehm_gc=no],
|
||||
[AC_MSG_ERROR([bad value $enableval for --enable-boehm-gc])] )],
|
||||
[with_boehm_gc=no])
|
||||
m4_divert_pop([INIT_PREPARE])dnl
|
||||
|
||||
AS_IF([test "x$with_boehm_gc" = "xyes"],
|
||||
AC_SEARCH_LIBS([GC_malloc], [gc],
|
||||
[AC_DEFINE(USE_BOEHM_GC, 1, [Define if Boehm GC is to be used])],
|
||||
[AC_MSG_FAILURE([--enable-boehm-gc specified but test for libgc failed])]))
|
||||
|
||||
|
||||
dnl LCOV
|
||||
dnl ====
|
||||
m4_divert_push([INIT_PREPARE])dnl
|
||||
@@ -955,9 +973,6 @@ AC_CONFIG_FILES(
|
||||
wrlib/Makefile wrlib/po/Makefile
|
||||
wrlib/tests/Makefile
|
||||
|
||||
dnl Rust implementation of WINGs libraries
|
||||
wutil-rs/Makefile
|
||||
|
||||
dnl WINGs toolkit
|
||||
WINGs/Makefile WINGs/WINGs/Makefile WINGs/po/Makefile
|
||||
WINGs/Documentation/Makefile WINGs/Resources/Makefile WINGs/Extras/Makefile
|
||||
|
||||
10
doc/build/Compilation.texi
vendored
10
doc/build/Compilation.texi
vendored
@@ -253,6 +253,14 @@ If found, then the library @emph{WRaster} can use the @emph{ImageMagick} library
|
||||
@sc{Window Maker} support more image formats, like @emph{SVG}, @emph{BMP}, @emph{TGA}, ...
|
||||
You can get it from @uref{http://www.imagemagick.org/}
|
||||
|
||||
@item @emph{Boehm GC}
|
||||
|
||||
This library can be used by the @emph{WINGs} utility toolkit to use a
|
||||
@cite{Boehm-Demers-Weiser Garbage Collector} instead of the traditional
|
||||
@command{malloc}/@command{free} functions from the @emph{libc}.
|
||||
You have to explicitly ask for its support though (@pxref{Configure Options}).
|
||||
You can get it from @uref{http://www.hboehm.info/gc/}
|
||||
|
||||
@end itemize
|
||||
|
||||
|
||||
@@ -460,6 +468,8 @@ You can find more information about the libraries in the
|
||||
@ref{Optional Dependencies}.
|
||||
|
||||
@table @option
|
||||
@item --enable-boehm-gc
|
||||
Never enabled by default, use Boehm GC instead of the default @emph{libc} @command{malloc()}
|
||||
|
||||
@item --disable-gif
|
||||
Disable GIF support in @emph{WRaster} library; when enabled use @file{libgif} or @file{libungif}.
|
||||
|
||||
@@ -134,7 +134,7 @@ else
|
||||
nodist_wmaker_SOURCES = misc.hack_nf.c \
|
||||
xmodifier.hack_nf.c
|
||||
|
||||
CLEANFILES = $(nodist_wmaker_SOURCES)
|
||||
CLEANFILES = $(nodist_wmaker_SOURCES) ../wmaker-rs/target
|
||||
|
||||
misc.hack_nf.c: misc.c $(top_srcdir)/script/nested-func-to-macro.sh
|
||||
$(AM_V_GEN)$(top_srcdir)/script/nested-func-to-macro.sh \
|
||||
@@ -160,7 +160,6 @@ wmaker_LDADD = \
|
||||
$(top_builddir)/WINGs/libWINGs.la\
|
||||
$(top_builddir)/WINGs/libWUtil.la\
|
||||
$(top_builddir)/wrlib/libwraster.la\
|
||||
$(top_builddir)/wutil-rs/target/debug/libwutil_rs.a\
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
@XLFLAGS@ \
|
||||
@LIBXRANDR@ \
|
||||
|
||||
@@ -139,7 +139,7 @@ static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int coun
|
||||
}
|
||||
wstrlcpy(title, &slist[*index][pos], sizeof(title));
|
||||
}
|
||||
data = malloc(sizeof(WAppMenuData));
|
||||
data = wmalloc(sizeof(WAppMenuData));
|
||||
if (data == NULL) {
|
||||
wwarning(_("appmenu: out of memory creating menu for window %lx"), win);
|
||||
wMenuDestroy(menu, True);
|
||||
@@ -152,7 +152,7 @@ static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int coun
|
||||
if (!entry) {
|
||||
wMenuDestroy(menu, True);
|
||||
wwarning(_("appmenu: out of memory creating menu for window %lx"), win);
|
||||
free(data);
|
||||
wfree(data);
|
||||
return NULL;
|
||||
}
|
||||
if (rtext[0] != 0)
|
||||
|
||||
31
src/dialog.c
31
src/dialog.c
@@ -39,10 +39,6 @@
|
||||
#include <time.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#ifdef HAVE_MALLOC_H
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
#include <signal.h>
|
||||
#ifdef __FreeBSD__
|
||||
#include <sys/signal.h>
|
||||
@@ -1361,7 +1357,7 @@ void wShowInfoPanel(WScreen *scr)
|
||||
char *posn = getPrettyOSName();
|
||||
if (posn) {
|
||||
snprintf(buffer, sizeof(buffer), _("Running on: %s (%s)\n"), posn, uts.machine);
|
||||
free(posn);
|
||||
wfree(posn);
|
||||
}
|
||||
else
|
||||
snprintf(buffer, sizeof(buffer), _("Running on: %s (%s)\n"), uts.sysname, uts.machine);
|
||||
@@ -1393,31 +1389,6 @@ void wShowInfoPanel(WScreen *scr)
|
||||
break;
|
||||
}
|
||||
|
||||
#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO2)
|
||||
{
|
||||
struct mallinfo2 ma = mallinfo2();
|
||||
snprintf(buffer, sizeof(buffer),
|
||||
#ifdef DEBUG
|
||||
_("Total memory allocated: %lu kB (in use: %lu kB, %lu free chunks)\n"),
|
||||
#else
|
||||
_("Total memory allocated: %lu kB (in use: %lu kB)\n"),
|
||||
#endif
|
||||
(ma.arena + ma.hblkhd) / 1024,
|
||||
(ma.uordblks + ma.hblkhd) / 1024
|
||||
#ifdef DEBUG
|
||||
/*
|
||||
* This information is representative of the memory
|
||||
* fragmentation. In ideal case it should be 1, but
|
||||
* that is never possible
|
||||
*/
|
||||
, ma.ordblks
|
||||
#endif
|
||||
);
|
||||
|
||||
strbuf = wstrappend(strbuf, buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
strbuf = wstrappend(strbuf, _("Image formats: "));
|
||||
strl = RSupportedFileFormats();
|
||||
separator = NULL;
|
||||
|
||||
12
src/dock.c
12
src/dock.c
@@ -3194,7 +3194,7 @@ static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
|
||||
setsid();
|
||||
#endif
|
||||
|
||||
args = malloc(sizeof(char *) * (argc + 1));
|
||||
args = wmalloc(sizeof(char *) * (argc + 1));
|
||||
if (!args)
|
||||
exit(111);
|
||||
|
||||
@@ -3338,8 +3338,8 @@ void wDockTrackWindowLaunch(WDock *dock, Window window)
|
||||
char *command = NULL;
|
||||
|
||||
if (!PropGetWMClass(window, &wm_class, &wm_instance)) {
|
||||
free(wm_class);
|
||||
free(wm_instance);
|
||||
wfree(wm_class);
|
||||
wfree(wm_instance);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -3419,10 +3419,8 @@ void wDockTrackWindowLaunch(WDock *dock, Window window)
|
||||
if (command)
|
||||
wfree(command);
|
||||
|
||||
if (wm_class)
|
||||
free(wm_class);
|
||||
if (wm_instance)
|
||||
free(wm_instance);
|
||||
wfree(wm_class);
|
||||
wfree(wm_instance);
|
||||
}
|
||||
|
||||
void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
|
||||
|
||||
@@ -141,7 +141,7 @@ WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
|
||||
{
|
||||
DeathHandler *handler;
|
||||
|
||||
handler = malloc(sizeof(DeathHandler));
|
||||
handler = wmalloc(sizeof(DeathHandler));
|
||||
if (!handler)
|
||||
return 0;
|
||||
|
||||
@@ -150,7 +150,7 @@ WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
|
||||
handler->client_data = cdata;
|
||||
|
||||
if (!deathHandlers)
|
||||
deathHandlers = WMCreateArrayWithDestructor(8, free);
|
||||
deathHandlers = WMCreateArrayWithDestructor(8, wfree);
|
||||
|
||||
WMAddToArray(deathHandlers, handler);
|
||||
|
||||
@@ -164,7 +164,7 @@ static void wdelete_death_handler(WMagicNumber id)
|
||||
if (!handler || !deathHandlers)
|
||||
return;
|
||||
|
||||
/* array destructor will call free(handler) */
|
||||
/* array destructor will call wfree(handler) */
|
||||
WMRemoveFromArray(deathHandlers, handler);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ WGeometryView *WCreateGeometryView(WMScreen * scr)
|
||||
widgetClass = W_RegisterUserWidget();
|
||||
}
|
||||
|
||||
gview = malloc(sizeof(WGeometryView));
|
||||
gview = wmalloc(sizeof(WGeometryView));
|
||||
if (!gview) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ static void setWVisualID(int screen, int val)
|
||||
/* no array at all, alloc space for screen + 1 entries
|
||||
* and init with default value */
|
||||
wVisualID_len = screen + 1;
|
||||
wVisualID = (int *)malloc(wVisualID_len * sizeof(int));
|
||||
wVisualID = (int *)wmalloc(wVisualID_len * sizeof(int));
|
||||
for (i = 0; i < wVisualID_len; i++) {
|
||||
wVisualID[i] = -1;
|
||||
}
|
||||
@@ -156,7 +156,7 @@ static void setWVisualID(int screen, int val)
|
||||
*/
|
||||
static int initWVisualID(const char *user_str)
|
||||
{
|
||||
char *mystr = strdup(user_str);
|
||||
char *mystr = wstrdup(user_str);
|
||||
int cur_in_pos = 0;
|
||||
int cur_out_pos = 0;
|
||||
int cur_screen = 0;
|
||||
@@ -191,7 +191,7 @@ static int initWVisualID(const char *user_str)
|
||||
cur_in_pos++;
|
||||
}
|
||||
|
||||
free(mystr);
|
||||
wfree(mystr);
|
||||
|
||||
if (cur_screen == 0||error_found != 0)
|
||||
return 1;
|
||||
@@ -383,7 +383,7 @@ Bool RelaunchWindow(WWindow *wwin)
|
||||
setsid();
|
||||
#endif
|
||||
/* argv is not null-terminated */
|
||||
char **a = (char **) malloc(argc + 1);
|
||||
char **a = (char **) wmalloc(argc + 1);
|
||||
if (! a) {
|
||||
werror("out of memory trying to relaunch the application");
|
||||
Exit(-1);
|
||||
@@ -624,6 +624,7 @@ static int real_main(int argc, char **argv)
|
||||
int d, s;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
wsetabort(wAbort);
|
||||
|
||||
/* for telling WPrefs what's the name of the wmaker binary being ran */
|
||||
setenv("WMAKER_BIN_NAME", argv[0], 1);
|
||||
|
||||
12
src/misc.c
12
src/misc.c
@@ -520,7 +520,7 @@ char *ExpandOptions(WScreen *scr, const char *cmdline)
|
||||
|
||||
len = strlen(cmdline);
|
||||
olen = len + 1;
|
||||
out = malloc(olen);
|
||||
out = wmalloc(olen);
|
||||
if (!out) {
|
||||
wwarning(_("out of memory during expansion of \"%s\""), cmdline);
|
||||
return NULL;
|
||||
@@ -573,7 +573,7 @@ char *ExpandOptions(WScreen *scr, const char *cmdline)
|
||||
(unsigned int)scr->focused_window->client_win);
|
||||
slen = strlen(tmpbuf);
|
||||
olen += slen;
|
||||
nout = realloc(out, olen);
|
||||
nout = wrealloc(out, olen);
|
||||
if (!nout) {
|
||||
wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%w", cmdline);
|
||||
goto error;
|
||||
@@ -590,7 +590,7 @@ char *ExpandOptions(WScreen *scr, const char *cmdline)
|
||||
snprintf(tmpbuf, sizeof(tmpbuf), "0x%x", (unsigned int)scr->current_workspace + 1);
|
||||
slen = strlen(tmpbuf);
|
||||
olen += slen;
|
||||
nout = realloc(out, olen);
|
||||
nout = wrealloc(out, olen);
|
||||
if (!nout) {
|
||||
wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%W", cmdline);
|
||||
goto error;
|
||||
@@ -607,7 +607,7 @@ char *ExpandOptions(WScreen *scr, const char *cmdline)
|
||||
if (user_input) {
|
||||
slen = strlen(user_input);
|
||||
olen += slen;
|
||||
nout = realloc(out, olen);
|
||||
nout = wrealloc(out, olen);
|
||||
if (!nout) {
|
||||
wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%a", cmdline);
|
||||
goto error;
|
||||
@@ -630,7 +630,7 @@ char *ExpandOptions(WScreen *scr, const char *cmdline)
|
||||
}
|
||||
slen = strlen(scr->xdestring);
|
||||
olen += slen;
|
||||
nout = realloc(out, olen);
|
||||
nout = wrealloc(out, olen);
|
||||
if (!nout) {
|
||||
wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%d", cmdline);
|
||||
goto error;
|
||||
@@ -651,7 +651,7 @@ char *ExpandOptions(WScreen *scr, const char *cmdline)
|
||||
}
|
||||
slen = strlen(selection);
|
||||
olen += slen;
|
||||
nout = realloc(out, olen);
|
||||
nout = wrealloc(out, olen);
|
||||
if (!nout) {
|
||||
wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%s", cmdline);
|
||||
goto error;
|
||||
|
||||
@@ -54,13 +54,13 @@ int PropGetWMClass(Window window, char **wm_class, char **wm_instance)
|
||||
|
||||
class_hint = XAllocClassHint();
|
||||
if (XGetClassHint(dpy, window, class_hint) == 0) {
|
||||
*wm_class = strdup("default");
|
||||
*wm_instance = strdup("default");
|
||||
*wm_class = wstrdup("default");
|
||||
*wm_instance = wstrdup("default");
|
||||
XFree(class_hint);
|
||||
return False;
|
||||
}
|
||||
*wm_instance = strdup(class_hint->res_name);
|
||||
*wm_class = strdup(class_hint->res_class);
|
||||
*wm_instance = wstrdup(class_hint->res_name);
|
||||
*wm_class = wstrdup(class_hint->res_class);
|
||||
|
||||
XFree(class_hint->res_name);
|
||||
XFree(class_hint->res_class);
|
||||
@@ -133,7 +133,7 @@ int PropGetGNUstepWMAttr(Window window, GNUstepWMAttributes ** attr)
|
||||
if (!data)
|
||||
return False;
|
||||
|
||||
*attr = malloc(sizeof(GNUstepWMAttributes));
|
||||
*attr = wmalloc(sizeof(GNUstepWMAttributes));
|
||||
if (!*attr) {
|
||||
XFree(data);
|
||||
return False;
|
||||
@@ -183,7 +183,7 @@ void PropSetIconTileHint(WScreen * scr, RImage * image)
|
||||
imageAtom = XInternAtom(dpy, "_RGBA_IMAGE", False);
|
||||
}
|
||||
|
||||
tmp = malloc(image->width * image->height * 4 + 4);
|
||||
tmp = wmalloc(image->width * image->height * 4 + 4);
|
||||
if (!tmp) {
|
||||
wwarning("could not allocate memory to set _WINDOWMAKER_ICON_TILE hint");
|
||||
return;
|
||||
|
||||
@@ -1243,7 +1243,7 @@ static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, co
|
||||
if (dentry->d_name[0] == '.')
|
||||
continue;
|
||||
|
||||
buffer = malloc(strlen(path[i]) + strlen(dentry->d_name) + 4);
|
||||
buffer = wmalloc(strlen(path[i]) + strlen(dentry->d_name) + 4);
|
||||
if (!buffer) {
|
||||
werror(_("out of memory while constructing directory menu %s"), path[i]);
|
||||
break;
|
||||
@@ -1288,7 +1288,7 @@ static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, co
|
||||
}
|
||||
}
|
||||
}
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
@@ -1315,7 +1315,7 @@ static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, co
|
||||
length += 7;
|
||||
if (command)
|
||||
length += strlen(command) + 6;
|
||||
buffer = malloc(length);
|
||||
buffer = wmalloc(length);
|
||||
if (!buffer) {
|
||||
werror(_("out of memory while constructing directory menu %s"), path[data->index]);
|
||||
break;
|
||||
@@ -1353,7 +1353,7 @@ static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, co
|
||||
if (command)
|
||||
length += strlen(command);
|
||||
|
||||
buffer = malloc(length);
|
||||
buffer = wmalloc(length);
|
||||
if (!buffer) {
|
||||
werror(_("out of memory while constructing directory menu %s"), path[data->index]);
|
||||
break;
|
||||
|
||||
@@ -289,9 +289,9 @@ static WMPropList *makeWindowState(WWindow * wwin, WApplication * wapp)
|
||||
}
|
||||
|
||||
if (instance)
|
||||
free(instance);
|
||||
wfree(instance);
|
||||
if (class)
|
||||
free(class);
|
||||
wfree(class);
|
||||
if (command)
|
||||
wfree(command);
|
||||
|
||||
@@ -382,7 +382,7 @@ static pid_t execCommand(WScreen *scr, char *command)
|
||||
|
||||
SetupEnvironment(scr);
|
||||
|
||||
args = malloc(sizeof(char *) * (argc + 1));
|
||||
args = wmalloc(sizeof(char *) * (argc + 1));
|
||||
if (!args)
|
||||
exit(111);
|
||||
for (i = 0; i < argc; i++) {
|
||||
|
||||
@@ -244,7 +244,7 @@ reinit:
|
||||
wApplicationSetBouncing(data->wapp, 0);
|
||||
WMDeleteTimerHandler(data->timer);
|
||||
wApplicationDestroy(data->wapp);
|
||||
free(data);
|
||||
wfree(data);
|
||||
}
|
||||
|
||||
static int bounceDirection(WAppIcon *aicon)
|
||||
@@ -324,7 +324,7 @@ void wAppBounce(WApplication *wapp)
|
||||
wApplicationIncrementRefcount(wapp);
|
||||
wApplicationSetBouncing(wapp, 1);
|
||||
|
||||
AppBouncerData *data = (AppBouncerData *)malloc(sizeof(AppBouncerData));
|
||||
AppBouncerData *data = (AppBouncerData *)wmalloc(sizeof(AppBouncerData));
|
||||
data->wapp = wapp;
|
||||
data->count = data->pow = 0;
|
||||
data->dir = bounceDirection(wApplicationGetAppIcon(wapp));
|
||||
|
||||
@@ -357,8 +357,7 @@ static void drawTitle(WSwitchPanel *panel, int idecks, const char *title)
|
||||
WMSetLabelText(panel->label, ntitle);
|
||||
}
|
||||
|
||||
if (ntitle)
|
||||
free(ntitle);
|
||||
wfree(ntitle);
|
||||
}
|
||||
|
||||
static WMArray *makeWindowListArray(WScreen *scr, int include_unmapped, Bool class_only)
|
||||
|
||||
12
src/window.c
12
src/window.c
@@ -950,10 +950,10 @@ WWindow *wManageWindow(WScreen *scr, Window window)
|
||||
}
|
||||
|
||||
if (instance)
|
||||
free(instance);
|
||||
wfree(instance);
|
||||
|
||||
if (class)
|
||||
free(class);
|
||||
wfree(class);
|
||||
#undef ADEQUATE
|
||||
}
|
||||
|
||||
@@ -2602,7 +2602,7 @@ void wWindowSetShape(WWindow * wwin)
|
||||
if (!rects)
|
||||
goto alt_code;
|
||||
|
||||
urec = malloc(sizeof(XRectangle) * (count + 2));
|
||||
urec = wmalloc(sizeof(XRectangle) * (count + 2));
|
||||
if (!urec) {
|
||||
XFree(rects);
|
||||
goto alt_code;
|
||||
@@ -2779,7 +2779,7 @@ WMagicNumber wWindowAddSavedState(const char *instance, const char *class,
|
||||
{
|
||||
WWindowState *wstate;
|
||||
|
||||
wstate = malloc(sizeof(WWindowState));
|
||||
wstate = wmalloc(sizeof(WWindowState));
|
||||
if (!wstate)
|
||||
return NULL;
|
||||
|
||||
@@ -2841,9 +2841,9 @@ WMagicNumber wWindowGetSavedState(Window win)
|
||||
if (command)
|
||||
wfree(command);
|
||||
if (instance)
|
||||
free(instance);
|
||||
wfree(instance);
|
||||
if (class)
|
||||
free(class);
|
||||
wfree(class);
|
||||
|
||||
return wstate;
|
||||
}
|
||||
|
||||
@@ -256,7 +256,7 @@ static void wXDNDGetTypeList(Display *dpy, Window window)
|
||||
return;
|
||||
}
|
||||
|
||||
typelist = malloc((count + 1) * sizeof(Atom));
|
||||
typelist = wmalloc((count + 1) * sizeof(Atom));
|
||||
a = (Atom *) data;
|
||||
for (i = 0; i < count; i++) {
|
||||
typelist[i] = a[i];
|
||||
@@ -267,7 +267,7 @@ static void wXDNDGetTypeList(Display *dpy, Window window)
|
||||
}
|
||||
typelist[count] = 0;
|
||||
XFree(data);
|
||||
free(typelist);
|
||||
wfree(typelist);
|
||||
}
|
||||
|
||||
Bool wXDNDProcessClientMessage(XClientMessageEvent *event)
|
||||
|
||||
@@ -18,52 +18,70 @@ AM_CPPFLAGS = \
|
||||
|
||||
liblist= @LIBRARY_SEARCH_PATH@ @INTLIBS@
|
||||
|
||||
wdwrite_LDADD = $(top_builddir)/WINGs/libWUtil.la $(liblist)
|
||||
wdwrite_LDADD = $(top_builddir)/WINGs/libWUtil.la \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
$(liblist)
|
||||
|
||||
wdread_LDADD = $(top_builddir)/WINGs/libWUtil.la $(liblist)
|
||||
wdread_LDADD = $(top_builddir)/WINGs/libWUtil.la \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
$(liblist)
|
||||
|
||||
wxcopy_LDADD = @XLFLAGS@ @XLIBS@
|
||||
wxcopy_LDADD = @XLFLAGS@ @XLIBS@ \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a
|
||||
|
||||
wxpaste_LDADD = @XLFLAGS@ @XLIBS@
|
||||
|
||||
getstyle_LDADD = $(top_builddir)/WINGs/libWUtil.la $(liblist)
|
||||
getstyle_LDADD = $(top_builddir)/WINGs/libWUtil.la\
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
$(liblist)
|
||||
|
||||
getstyle_SOURCES = getstyle.c fontconv.c common.h
|
||||
|
||||
setstyle_LDADD = \
|
||||
$(top_builddir)/WINGs/libWUtil.la \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
@XLFLAGS@ @XLIBS@ $(liblist)
|
||||
|
||||
setstyle_SOURCES = setstyle.c fontconv.c common.h
|
||||
|
||||
convertfonts_LDADD = $(top_builddir)/WINGs/libWUtil.la $(liblist)
|
||||
convertfonts_LDADD = $(top_builddir)/WINGs/libWUtil.la \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
$(liblist)
|
||||
|
||||
convertfonts_SOURCES = convertfonts.c fontconv.c common.h
|
||||
|
||||
seticons_LDADD= $(top_builddir)/WINGs/libWUtil.la $(liblist)
|
||||
seticons_LDADD= $(top_builddir)/WINGs/libWUtil.la\
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
$(liblist)
|
||||
|
||||
geticonset_LDADD= $(top_builddir)/WINGs/libWUtil.la $(liblist)
|
||||
geticonset_LDADD= $(top_builddir)/WINGs/libWUtil.la\
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
$(liblist)
|
||||
|
||||
wmagnify_LDADD = \
|
||||
$(top_builddir)/WINGs/libWINGs.la \
|
||||
$(top_builddir)/WINGs/libWUtil.la \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
$(top_builddir)/wrlib/libwraster.la \
|
||||
@XLFLAGS@ @XLIBS@ @INTLIBS@
|
||||
|
||||
wmsetbg_LDADD = \
|
||||
$(top_builddir)/WINGs/libWINGs.la \
|
||||
$(top_builddir)/WINGs/libWUtil.la \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
$(top_builddir)/wrlib/libwraster.la \
|
||||
@XLFLAGS@ @LIBXINERAMA@ @XLIBS@ @INTLIBS@
|
||||
|
||||
wmgenmenu_LDADD = \
|
||||
$(top_builddir)/WINGs/libWUtil.la \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
@INTLIBS@
|
||||
|
||||
wmgenmenu_SOURCES = wmgenmenu.c wmgenmenu.h
|
||||
|
||||
wmmenugen_LDADD = \
|
||||
$(top_builddir)/WINGs/libWUtil.la \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
@INTLIBS@
|
||||
|
||||
wmmenugen_SOURCES = wmmenugen.c wmmenugen.h wmmenugen_misc.c \
|
||||
@@ -75,6 +93,7 @@ wmiv_CFLAGS = @PANGO_CFLAGS@ @PTHREAD_CFLAGS@
|
||||
wmiv_LDADD = \
|
||||
$(top_builddir)/wrlib/libwraster.la \
|
||||
$(top_builddir)/WINGs/libWINGs.la \
|
||||
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
|
||||
@XLFLAGS@ @XLIBS@ @GFXLIBS@ \
|
||||
@PANGO_LIBS@ @PTHREAD_LIBS@ @LIBEXIF@
|
||||
|
||||
|
||||
@@ -242,7 +242,7 @@ static void makeThemePack(WMPropList * style, const char *themeName)
|
||||
|
||||
WMDeleteFromPLArray(value, 1);
|
||||
WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
|
||||
free(newPath);
|
||||
wfree(newPath);
|
||||
} else {
|
||||
findCopyFile(themeDir, WMGetFromPLString(file));
|
||||
}
|
||||
@@ -262,7 +262,7 @@ static void makeThemePack(WMPropList * style, const char *themeName)
|
||||
|
||||
WMDeleteFromPLArray(value, 1);
|
||||
WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
|
||||
free(newPath);
|
||||
wfree(newPath);
|
||||
} else {
|
||||
findCopyFile(themeDir, WMGetFromPLString(file));
|
||||
}
|
||||
@@ -277,7 +277,7 @@ static void makeThemePack(WMPropList * style, const char *themeName)
|
||||
|
||||
WMDeleteFromPLArray(value, 2);
|
||||
WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
|
||||
free(newPath);
|
||||
wfree(newPath);
|
||||
} else {
|
||||
findCopyFile(themeDir, WMGetFromPLString(file));
|
||||
}
|
||||
|
||||
18
util/wmiv.c
18
util/wmiv.c
@@ -202,7 +202,7 @@ int change_title(XTextProperty *prop, char *filename)
|
||||
XSetWMName(dpy, win, prop);
|
||||
if (prop->value)
|
||||
XFree(prop->value);
|
||||
free(combined_title);
|
||||
wfree(combined_title);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -596,8 +596,8 @@ int linked_list_add(linked_list_t *list, const void *data)
|
||||
{
|
||||
link_t *link;
|
||||
|
||||
/* calloc sets the "next" field to zero. */
|
||||
link = calloc(1, sizeof(link_t));
|
||||
/* wmalloc zeros the buffer it returns, so the "next" is zero. */
|
||||
link = wmalloc(sizeof(link_t));
|
||||
if (!link) {
|
||||
fprintf(stderr, "Error: memory allocation failed\n");
|
||||
return EXIT_FAILURE;
|
||||
@@ -627,8 +627,8 @@ void linked_list_free(linked_list_t *list)
|
||||
/* Store the next value so that we don't access freed memory. */
|
||||
next = link->next;
|
||||
if (link->data)
|
||||
free((char *)link->data);
|
||||
free(link);
|
||||
wfree((char *)link->data);
|
||||
wfree(link);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -651,7 +651,7 @@ link_t *connect_dir(char *dirpath, linked_list_t *li)
|
||||
/* maybe it's a file */
|
||||
struct stat stDirInfo;
|
||||
if (lstat(dirpath, &stDirInfo) == 0) {
|
||||
linked_list_add(li, strdup(dirpath));
|
||||
linked_list_add(li, wstrdup(dirpath));
|
||||
return li->first;
|
||||
} else {
|
||||
return NULL;
|
||||
@@ -664,11 +664,11 @@ link_t *connect_dir(char *dirpath, linked_list_t *li)
|
||||
else
|
||||
snprintf(path, PATH_MAX, "%s%c%s", dirpath, FILE_SEPARATOR, dir[idx]->d_name);
|
||||
|
||||
free(dir[idx]);
|
||||
wfree(dir[idx]);
|
||||
if ((lstat(path, &stDirInfo) == 0) && !S_ISDIR(stDirInfo.st_mode))
|
||||
linked_list_add(li, strdup(path));
|
||||
linked_list_add(li, wstrdup(path));
|
||||
}
|
||||
free(dir);
|
||||
wfree(dir);
|
||||
return li->first;
|
||||
}
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ void parse_locale(const char *what, char **language, char **country, char **enco
|
||||
*language = wstrdup(e);
|
||||
|
||||
out:
|
||||
free(e);
|
||||
wfree(e);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
@@ -398,7 +398,7 @@ static BackgroundTexture *parseTexture(RContext * rc, char *text)
|
||||
RGradientStyle gtype;
|
||||
int iwidth, iheight;
|
||||
|
||||
colors = malloc(sizeof(RColor *) * (count - 1));
|
||||
colors = wmalloc(sizeof(RColor *) * (count - 1));
|
||||
if (!colors) {
|
||||
wwarning("out of memory while parsing texture");
|
||||
goto error;
|
||||
@@ -425,7 +425,7 @@ static BackgroundTexture *parseTexture(RContext * rc, char *text)
|
||||
wfree(colors);
|
||||
goto error;
|
||||
}
|
||||
if (!(colors[i - 2] = malloc(sizeof(RColor)))) {
|
||||
if (!(colors[i - 2] = wmalloc(sizeof(RColor)))) {
|
||||
wwarning("out of memory while parsing texture");
|
||||
|
||||
for (j = 0; colors[j] != NULL; j++)
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xatom.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "../src/wconfig.h"
|
||||
|
||||
#define LINESIZE (4*1024)
|
||||
@@ -197,7 +199,7 @@ int main(int argc, char **argv)
|
||||
break;
|
||||
}
|
||||
if (buf_len == 0) {
|
||||
nbuf = malloc(buf_len = l + nl + 1);
|
||||
nbuf = wmalloc(buf_len = l + nl + 1);
|
||||
} else if (buf_len < l + nl + 1) {
|
||||
/*
|
||||
* To avoid terrible performance on big input buffers,
|
||||
@@ -205,12 +207,7 @@ int main(int argc, char **argv)
|
||||
* current line.
|
||||
*/
|
||||
buf_len = 2 * buf_len + nl + 1;
|
||||
/* some realloc implementations don't do malloc if buf==NULL */
|
||||
if (buf == NULL) {
|
||||
nbuf = malloc(buf_len);
|
||||
} else {
|
||||
nbuf = realloc(buf, buf_len);
|
||||
}
|
||||
nbuf = wrealloc(buf, buf_len);
|
||||
} else {
|
||||
nbuf = buf;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
pub mod application;
|
||||
pub mod app_icon;
|
||||
pub mod app_menu;
|
||||
pub mod application;
|
||||
pub mod defaults;
|
||||
pub mod dock;
|
||||
pub mod global;
|
||||
pub mod icon;
|
||||
pub mod memory;
|
||||
pub mod menu;
|
||||
pub mod properties;
|
||||
pub mod screen;
|
||||
|
||||
180
wmaker-rs/src/memory.rs
Normal file
180
wmaker-rs/src/memory.rs
Normal file
@@ -0,0 +1,180 @@
|
||||
//! Provides an FFI-compatible allocator for C.
|
||||
//!
|
||||
//! This replaces the bulk of the code form WINGs/memory.c, but
|
||||
//! it should go away once we're in a place that we don't need
|
||||
//! to rely on `malloc` anymore in C code.
|
||||
|
||||
use std::alloc::{Layout, alloc_zeroed, dealloc, realloc};
|
||||
use std::collections::BTreeMap;
|
||||
use std::ffi::c_void;
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Allocation {
|
||||
layout: Layout,
|
||||
rc: u32,
|
||||
}
|
||||
|
||||
type WMallocMap = BTreeMap<usize, Allocation>;
|
||||
|
||||
static ALLOCS: Mutex<WMallocMap> = Mutex::new(WMallocMap::new());
|
||||
const ALIGN: usize = 64;
|
||||
|
||||
/// A wrapper around the Rust allocator API's `alloc_zeroed`
|
||||
/// function for FFI.
|
||||
///
|
||||
/// Note: This will always return nil on a zero-sized
|
||||
/// allocation. Don't try to use this to allocate Rust ZSTs;
|
||||
/// it is only suitable for FFI.
|
||||
///
|
||||
/// # Safety
|
||||
/// This will return a 64-byte aligned pointer of the specified
|
||||
/// length, or nil if the allocation cannot be satisfied. It is
|
||||
/// the caller's responsibility to ensure that the result is not
|
||||
/// null, to avoid violating memory safety, and to make sure
|
||||
/// that any alignment for any value the pointer is used to
|
||||
/// refer to is suitably aligned.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wmalloc(size: usize) -> *mut c_void {
|
||||
if size == 0 {
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
let layout = Layout::from_size_align(size, ALIGN).expect("layout makes sense");
|
||||
let ptr = unsafe { alloc_zeroed(layout) };
|
||||
if ptr.is_null() {
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
let rc = 1;
|
||||
let alloc = Allocation { layout, rc };
|
||||
let mut allocs = ALLOCS.lock().expect("lock not poisoned");
|
||||
allocs.insert(ptr.addr(), alloc);
|
||||
ptr.cast()
|
||||
}
|
||||
|
||||
/// A wrapper around the Rust allocator API's `dealloc` function
|
||||
/// for FFI.
|
||||
///
|
||||
/// If the pointer argument is nil, this is a nop. Otherwise,
|
||||
/// it must have been allocated by `wmalloc` or `wrealloc`.
|
||||
///
|
||||
/// # Safety
|
||||
/// The caller must ensure that the pointer that is passed to
|
||||
/// this function was allocated by `wmalloc`. Note that this
|
||||
/// will not free unless this was the allocation's last
|
||||
/// reference.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wfree(ptr: *mut c_void) {
|
||||
if ptr.is_null() {
|
||||
return;
|
||||
}
|
||||
let alloc = {
|
||||
let mut allocs = ALLOCS.lock().expect("wfree: allocs is unpoisoned");
|
||||
let addr = ptr.addr();
|
||||
let alloc = allocs.get_mut(&addr).expect("wfree: allocs records ptr");
|
||||
alloc.rc -= 1;
|
||||
let alloc = alloc.clone();
|
||||
if alloc.rc == 0 {
|
||||
allocs.remove(&addr).expect("wfree: allocs unaltered");
|
||||
}
|
||||
alloc
|
||||
};
|
||||
// We can call `dealloc` without holding the ALLOCS lock.
|
||||
if alloc.rc == 0 {
|
||||
unsafe {
|
||||
dealloc(ptr.cast(), alloc.layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper around the Rust allocator API's `realloc` function
|
||||
/// for FFI calls.
|
||||
///
|
||||
/// Note that this attempts will always free and return NULL on
|
||||
/// a reallocation of size 0, so do not try to use this to
|
||||
/// allocate Rust ZSTs: it is purely for FFI. If the source
|
||||
/// pointer is nil and size is positive, then the behavior is
|
||||
/// that of `wmalloc`.
|
||||
///
|
||||
/// The pointer argument just have been allocated previously
|
||||
/// with `wmalloc` or `wrealloc`.
|
||||
///
|
||||
/// If successful, returns a non-nil pointer, and the old
|
||||
/// pointer should be considered invalid, and must not be
|
||||
/// dereferenced. If the old size was smaller than the new
|
||||
/// size, elements, elements after the old size are zeroed.
|
||||
/// Elements between the start of the array and the minimum of
|
||||
/// the old and new sizes are unchanged.
|
||||
///
|
||||
/// On failure, returns nil, and does not free the source
|
||||
/// pointer or otherwise alter its contents.
|
||||
///
|
||||
/// # Safety
|
||||
/// This will return a 64-byte aligned pointer of the specified
|
||||
/// length, or nil if the allocation cannot be satisfied. It is
|
||||
/// the caller's responsibility to ensure that the result is not
|
||||
/// null, to avoid violating memory safety, and to make sure
|
||||
/// that any alignment for any value the pointer is used to
|
||||
/// refer to is suitably aligned.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wrealloc(ptr: *mut c_void, size: usize) -> *mut c_void {
|
||||
if size == 0 {
|
||||
unsafe {
|
||||
wfree(ptr);
|
||||
}
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
if ptr.is_null() {
|
||||
return unsafe { wmalloc(size) };
|
||||
}
|
||||
let new_layout = Layout::from_size_align(size, ALIGN).expect("layout ok");
|
||||
let addr = ptr.addr();
|
||||
let old_layout = {
|
||||
let allocs = ALLOCS.lock().expect("wrealloc: allocs is unpoisoned");
|
||||
let alloc = allocs.get(&addr).expect("wrealloc: allocs records ptr");
|
||||
alloc.layout
|
||||
};
|
||||
let old_size = old_layout.size();
|
||||
let ptr = unsafe { realloc(ptr.cast(), old_layout, size) };
|
||||
if ptr.is_null() {
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
{
|
||||
let mut allocs = ALLOCS.lock().expect("wrealloc: allocs still unpoisoned");
|
||||
let alloc = allocs.get_mut(&addr).expect("wmrealloc: allocs knows ptr");
|
||||
alloc.layout = new_layout;
|
||||
}
|
||||
if old_size < size {
|
||||
let zlen = size - old_size;
|
||||
let zptr = ptr.wrapping_add(old_size);
|
||||
unsafe {
|
||||
std::ptr::write_bytes(zptr, 0, zlen);
|
||||
}
|
||||
}
|
||||
ptr.cast()
|
||||
}
|
||||
|
||||
/// Increments the reference count on `ptr`.
|
||||
///
|
||||
/// # Safety
|
||||
/// The caller must insure that `ptr` refers to a valid, active
|
||||
/// allocation that was made with `wmalloc` or `wrealloc`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wretain(ptr: *mut c_void) -> *mut c_void {
|
||||
let addr = ptr.addr();
|
||||
let mut allocs = ALLOCS.lock().expect("wretain: allocs is unpoisoned");
|
||||
let alloc = allocs.get_mut(&addr).expect("wretain: allocs records ptr");
|
||||
alloc.rc += 1;
|
||||
ptr
|
||||
}
|
||||
|
||||
/// Decrements the reference count on `ptr`, and frees if it
|
||||
/// reaches 0, but wrapping `wfree`.
|
||||
///
|
||||
/// # Safety
|
||||
/// `ptr` must be valid as an argument to `wfree`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wrelease(ptr: *mut c_void) {
|
||||
unsafe {
|
||||
wfree(ptr);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,8 @@ lib_LTLIBRARIES = libWMaker.la
|
||||
|
||||
include_HEADERS = WMaker.h
|
||||
|
||||
AM_CPPFLAGS = $(DFLAGS) @XCFLAGS@
|
||||
AM_CPPFLAGS = $(DFLAGS) @XCFLAGS@ \
|
||||
-I$(top_srcdir)/WINGs -I$(top_builddir)/WINGs
|
||||
|
||||
libWMaker_la_LIBADD = @XLFLAGS@ @XLIBS@
|
||||
|
||||
|
||||
12
wmlib/app.c
12
wmlib/app.c
@@ -24,6 +24,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "WMaker.h"
|
||||
#include "app.h"
|
||||
|
||||
@@ -31,7 +33,7 @@ WMAppContext *WMAppCreateWithMain(Display * display, int screen_number, Window m
|
||||
{
|
||||
wmAppContext *ctx;
|
||||
|
||||
ctx = malloc(sizeof(wmAppContext));
|
||||
ctx = wmalloc(sizeof(wmAppContext));
|
||||
if (!ctx)
|
||||
return NULL;
|
||||
|
||||
@@ -39,9 +41,9 @@ WMAppContext *WMAppCreateWithMain(Display * display, int screen_number, Window m
|
||||
ctx->screen_number = screen_number;
|
||||
ctx->our_leader_hint = False;
|
||||
ctx->main_window = main_window;
|
||||
ctx->windows = malloc(sizeof(Window));
|
||||
ctx->windows = wmalloc(sizeof(Window));
|
||||
if (!ctx->windows) {
|
||||
free(ctx);
|
||||
wfree(ctx);
|
||||
return NULL;
|
||||
}
|
||||
ctx->win_count = 1;
|
||||
@@ -58,13 +60,13 @@ int WMAppAddWindow(WMAppContext * app, Window window)
|
||||
{
|
||||
Window *win;
|
||||
|
||||
win = malloc(sizeof(Window) * (app->win_count + 1));
|
||||
win = wmalloc(sizeof(Window) * (app->win_count + 1));
|
||||
if (!win)
|
||||
return False;
|
||||
|
||||
memcpy(win, app->windows, sizeof(Window) * app->win_count);
|
||||
|
||||
free(app->windows);
|
||||
wfree(app->windows);
|
||||
|
||||
win[app->win_count] = window;
|
||||
app->windows = win;
|
||||
|
||||
30
wmlib/menu.c
30
wmlib/menu.c
@@ -26,6 +26,8 @@
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "WMaker.h"
|
||||
#include "app.h"
|
||||
#include "menu.h"
|
||||
@@ -37,7 +39,7 @@ WMMenu *WMMenuCreate(WMAppContext * app, char *title)
|
||||
if (strlen(title) > 255)
|
||||
return NULL;
|
||||
|
||||
menu = malloc(sizeof(wmMenu));
|
||||
menu = wmalloc(sizeof(wmMenu));
|
||||
if (!menu)
|
||||
return NULL;
|
||||
|
||||
@@ -50,12 +52,12 @@ WMMenu *WMMenuCreate(WMAppContext * app, char *title)
|
||||
menu->realized = False;
|
||||
menu->code = app->last_menu_tag++;
|
||||
|
||||
menu->entryline = malloc(strlen(title) + 32);
|
||||
menu->entryline2 = malloc(32);
|
||||
menu->entryline = wmalloc(strlen(title) + 32);
|
||||
menu->entryline2 = wmalloc(32);
|
||||
if (!menu->entryline || !menu->entryline2) {
|
||||
if (menu->entryline)
|
||||
free(menu->entryline);
|
||||
free(menu);
|
||||
wfree(menu->entryline);
|
||||
wfree(menu);
|
||||
return NULL;
|
||||
}
|
||||
sprintf(menu->entryline, "%i %i %s", wmBeginMenu, menu->code, title);
|
||||
@@ -77,13 +79,13 @@ WMMenuAddItem(WMMenu * menu, char *text, WMMenuAction action,
|
||||
if (strlen(text) > 255)
|
||||
return -1;
|
||||
|
||||
entry = malloc(sizeof(wmMenuEntry));
|
||||
entry = wmalloc(sizeof(wmMenuEntry));
|
||||
if (!entry)
|
||||
return -1;
|
||||
|
||||
entry->entryline = malloc(strlen(text) + 100);
|
||||
entry->entryline = wmalloc(strlen(text) + 100);
|
||||
if (!entry->entryline) {
|
||||
free(entry);
|
||||
wfree(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -125,13 +127,13 @@ int WMMenuAddSubmenu(WMMenu * menu, char *text, WMMenu * submenu)
|
||||
if (strlen(text) > 255)
|
||||
return -1;
|
||||
|
||||
entry = malloc(sizeof(wmMenuEntry));
|
||||
entry = wmalloc(sizeof(wmMenuEntry));
|
||||
if (!entry)
|
||||
return -1;
|
||||
|
||||
entry->entryline = malloc(strlen(text) + 100);
|
||||
entry->entryline = wmalloc(strlen(text) + 100);
|
||||
if (!entry->entryline) {
|
||||
free(entry);
|
||||
wfree(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -217,7 +219,7 @@ int WMRealizeMenus(WMAppContext * app)
|
||||
return True;
|
||||
|
||||
count++;
|
||||
slist = malloc(count * sizeof(char *));
|
||||
slist = wmalloc(count * sizeof(char *));
|
||||
if (!slist) {
|
||||
return False;
|
||||
}
|
||||
@@ -227,10 +229,10 @@ int WMRealizeMenus(WMAppContext * app)
|
||||
addItems(slist, &i, app->main_menu);
|
||||
|
||||
if (!XStringListToTextProperty(slist, i, &text_prop)) {
|
||||
free(slist);
|
||||
wfree(slist);
|
||||
return False;
|
||||
}
|
||||
free(slist);
|
||||
wfree(slist);
|
||||
XSetTextProperty(app->dpy, app->main_window, &text_prop, getatom(app->dpy));
|
||||
|
||||
XFree(text_prop.value);
|
||||
|
||||
@@ -82,7 +82,8 @@ libwraster_la_SOURCES += load_magick.c
|
||||
endif
|
||||
|
||||
AM_CFLAGS = @MAGICKFLAGS@
|
||||
AM_CPPFLAGS = $(DFLAGS) @HEADER_SEARCH_PATH@
|
||||
AM_CPPFLAGS = $(DFLAGS) @HEADER_SEARCH_PATH@ \
|
||||
-I$(top_srcdir)/WINGs -I$(top_builddir)/WINGs
|
||||
|
||||
libwraster_la_LIBADD = @LIBRARY_SEARCH_PATH@ @GFXLIBS@ @MAGICKLIBS@ @XLIBS@ @LIBXMU@ -lm
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "scale.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -104,17 +106,17 @@ static Bool allocateStandardPseudoColor(RContext * ctx, XStandardColormap * stdc
|
||||
return False;
|
||||
}
|
||||
|
||||
ctx->colors = malloc(sizeof(XColor) * ctx->ncolors);
|
||||
ctx->colors = wmalloc(sizeof(XColor) * ctx->ncolors);
|
||||
if (!ctx->colors) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
|
||||
return False;
|
||||
}
|
||||
|
||||
ctx->pixels = malloc(sizeof(unsigned long) * ctx->ncolors);
|
||||
ctx->pixels = wmalloc(sizeof(unsigned long) * ctx->ncolors);
|
||||
if (!ctx->pixels) {
|
||||
|
||||
free(ctx->colors);
|
||||
wfree(ctx->colors);
|
||||
ctx->colors = NULL;
|
||||
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
@@ -246,15 +248,15 @@ static Bool allocatePseudoColor(RContext *ctx)
|
||||
|
||||
assert(cpc >= 2 && ncolors <= (1 << ctx->depth));
|
||||
|
||||
colors = malloc(sizeof(XColor) * ncolors);
|
||||
colors = wmalloc(sizeof(XColor) * ncolors);
|
||||
if (!colors) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return False;
|
||||
}
|
||||
|
||||
ctx->pixels = malloc(sizeof(unsigned long) * ncolors);
|
||||
ctx->pixels = wmalloc(sizeof(unsigned long) * ncolors);
|
||||
if (!ctx->pixels) {
|
||||
free(colors);
|
||||
wfree(colors);
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return False;
|
||||
}
|
||||
@@ -343,7 +345,7 @@ static XColor *allocateGrayScale(RContext * ctx)
|
||||
ctx->attribs->render_mode = RBestMatchRendering;
|
||||
}
|
||||
|
||||
colors = malloc(sizeof(XColor) * ncolors);
|
||||
colors = wmalloc(sizeof(XColor) * ncolors);
|
||||
if (!colors) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return False;
|
||||
@@ -526,7 +528,7 @@ RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttribu
|
||||
RContext *context;
|
||||
XGCValues gcv;
|
||||
|
||||
context = malloc(sizeof(RContext));
|
||||
context = wmalloc(sizeof(RContext));
|
||||
if (!context) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return NULL;
|
||||
@@ -537,9 +539,9 @@ RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttribu
|
||||
|
||||
context->screen_number = screen_number;
|
||||
|
||||
context->attribs = malloc(sizeof(RContextAttributes));
|
||||
context->attribs = wmalloc(sizeof(RContextAttributes));
|
||||
if (!context->attribs) {
|
||||
free(context);
|
||||
wfree(context);
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return NULL;
|
||||
}
|
||||
@@ -568,7 +570,7 @@ RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttribu
|
||||
templ.visualid = context->attribs->visualid;
|
||||
vinfo = XGetVisualInfo(context->dpy, VisualIDMask | VisualScreenMask, &templ, &nret);
|
||||
if (!vinfo || nret == 0) {
|
||||
free(context);
|
||||
wfree(context);
|
||||
RErrorCode = RERR_BADVISUALID;
|
||||
return NULL;
|
||||
}
|
||||
@@ -615,13 +617,13 @@ RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttribu
|
||||
|
||||
if (context->vclass == PseudoColor || context->vclass == StaticColor) {
|
||||
if (!setupPseudoColorColormap(context)) {
|
||||
free(context);
|
||||
wfree(context);
|
||||
return NULL;
|
||||
}
|
||||
} else if (context->vclass == GrayScale || context->vclass == StaticGray) {
|
||||
context->colors = allocateGrayScale(context);
|
||||
if (!context->colors) {
|
||||
free(context);
|
||||
wfree(context);
|
||||
return NULL;
|
||||
}
|
||||
} else if (context->vclass == TrueColor) {
|
||||
@@ -668,9 +670,9 @@ void RDestroyContext(RContext *context)
|
||||
if ((context->attribs->flags & RC_VisualID) &&
|
||||
!(context->attribs->flags & RC_DefaultVisual))
|
||||
XDestroyWindow(context->dpy, context->drawable);
|
||||
free(context->attribs);
|
||||
wfree(context->attribs);
|
||||
}
|
||||
free(context);
|
||||
wfree(context);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,13 +33,15 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "convert.h"
|
||||
#include "xutil.h"
|
||||
#include "wr_i18n.h"
|
||||
|
||||
|
||||
#define NFREE(n) if (n) free(n)
|
||||
#define NFREE(n) wfree(n)
|
||||
|
||||
#define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
|
||||
|
||||
@@ -70,7 +72,7 @@ static void release_conversion_table(void)
|
||||
RConversionTable *tmp_to_delete = tmp;
|
||||
|
||||
tmp = tmp->next;
|
||||
free(tmp_to_delete);
|
||||
wfree(tmp_to_delete);
|
||||
}
|
||||
conversionTable = NULL;
|
||||
}
|
||||
@@ -83,7 +85,7 @@ static void release_std_conversion_table(void)
|
||||
RStdConversionTable *tmp_to_delete = tmp;
|
||||
|
||||
tmp = tmp->next;
|
||||
free(tmp_to_delete);
|
||||
wfree(tmp_to_delete);
|
||||
}
|
||||
stdConversionTable = NULL;
|
||||
}
|
||||
@@ -108,7 +110,7 @@ static unsigned short *computeTable(unsigned short mask)
|
||||
if (tmp)
|
||||
return tmp->table;
|
||||
|
||||
tmp = (RConversionTable *) malloc(sizeof(RConversionTable));
|
||||
tmp = (RConversionTable *) wmalloc(sizeof(RConversionTable));
|
||||
if (tmp == NULL)
|
||||
return NULL;
|
||||
|
||||
@@ -135,7 +137,7 @@ static unsigned int *computeStdTable(unsigned int mult, unsigned int max)
|
||||
if (tmp)
|
||||
return tmp->table;
|
||||
|
||||
tmp = (RStdConversionTable *) malloc(sizeof(RStdConversionTable));
|
||||
tmp = (RStdConversionTable *) wmalloc(sizeof(RStdConversionTable));
|
||||
if (tmp == NULL)
|
||||
return NULL;
|
||||
|
||||
@@ -372,8 +374,8 @@ static RXImage *image2TrueColor(RContext * ctx, RImage * image)
|
||||
signed char *nerr;
|
||||
int ch = (HAS_ALPHA(image) ? 4 : 3);
|
||||
|
||||
err = malloc(ch * (image->width + 2));
|
||||
nerr = malloc(ch * (image->width + 2));
|
||||
err = wmalloc(ch * (image->width + 2));
|
||||
nerr = wmalloc(ch * (image->width + 2));
|
||||
if (!err || !nerr) {
|
||||
NFREE(err);
|
||||
NFREE(nerr);
|
||||
@@ -387,8 +389,8 @@ static RXImage *image2TrueColor(RContext * ctx, RImage * image)
|
||||
|
||||
convertTrueColor_generic(ximg, image, err, nerr,
|
||||
rtable, gtable, btable, dr, dg, db, roffs, goffs, boffs);
|
||||
free(err);
|
||||
free(nerr);
|
||||
wfree(err);
|
||||
wfree(nerr);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -540,8 +542,8 @@ static RXImage *image2PseudoColor(RContext * ctx, RImage * image)
|
||||
#ifdef WRLIB_DEBUG
|
||||
fprintf(stderr, "pseudo color dithering with %d colors per channel\n", cpc);
|
||||
#endif
|
||||
err = malloc(4 * (image->width + 3));
|
||||
nerr = malloc(4 * (image->width + 3));
|
||||
err = wmalloc(4 * (image->width + 3));
|
||||
nerr = wmalloc(4 * (image->width + 3));
|
||||
if (!err || !nerr) {
|
||||
NFREE(err);
|
||||
NFREE(nerr);
|
||||
@@ -555,8 +557,8 @@ static RXImage *image2PseudoColor(RContext * ctx, RImage * image)
|
||||
convertPseudoColor_to_8(ximg, image, err + 4, nerr + 4,
|
||||
rtable, gtable, btable, dr, dg, db, ctx->pixels, cpc);
|
||||
|
||||
free(err);
|
||||
free(nerr);
|
||||
wfree(err);
|
||||
wfree(nerr);
|
||||
}
|
||||
|
||||
return ximg;
|
||||
@@ -618,8 +620,8 @@ static RXImage *image2StandardPseudoColor(RContext * ctx, RImage * image)
|
||||
fprintf(stderr, "pseudo color dithering with %d colors per channel\n",
|
||||
ctx->attribs->colors_per_channel);
|
||||
#endif
|
||||
err = (short *)malloc(3 * (image->width + 2) * sizeof(short));
|
||||
nerr = (short *)malloc(3 * (image->width + 2) * sizeof(short));
|
||||
err = (short *)wmalloc(3 * (image->width + 2) * sizeof(short));
|
||||
nerr = (short *)wmalloc(3 * (image->width + 2) * sizeof(short));
|
||||
if (!err || !nerr) {
|
||||
NFREE(err);
|
||||
NFREE(nerr);
|
||||
@@ -707,8 +709,8 @@ static RXImage *image2StandardPseudoColor(RContext * ctx, RImage * image)
|
||||
|
||||
ofs += ximg->image->bytes_per_line - image->width;
|
||||
}
|
||||
free(err);
|
||||
free(nerr);
|
||||
wfree(err);
|
||||
wfree(nerr);
|
||||
}
|
||||
ximg->image->data = (char *)data;
|
||||
|
||||
@@ -773,8 +775,8 @@ static RXImage *image2GrayScale(RContext * ctx, RImage * image)
|
||||
#ifdef WRLIB_DEBUG
|
||||
fprintf(stderr, "grayscale dither with %d colors per channel\n", cpc);
|
||||
#endif
|
||||
gerr = (short *)malloc((image->width + 2) * sizeof(short));
|
||||
ngerr = (short *)malloc((image->width + 2) * sizeof(short));
|
||||
gerr = (short *)wmalloc((image->width + 2) * sizeof(short));
|
||||
ngerr = (short *)wmalloc((image->width + 2) * sizeof(short));
|
||||
if (!gerr || !ngerr) {
|
||||
NFREE(gerr);
|
||||
NFREE(ngerr);
|
||||
@@ -830,8 +832,8 @@ static RXImage *image2GrayScale(RContext * ctx, RImage * image)
|
||||
gerr = ngerr;
|
||||
ngerr = terr;
|
||||
}
|
||||
free(gerr);
|
||||
free(ngerr);
|
||||
wfree(gerr);
|
||||
wfree(ngerr);
|
||||
}
|
||||
ximg->image->data = (char *)data;
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
#include <string.h>
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "wr_i18n.h"
|
||||
|
||||
@@ -46,7 +48,7 @@ int RBlurImage(RImage * image)
|
||||
unsigned char *pptr = NULL, *tmpp;
|
||||
int ch = image->format == RRGBAFormat ? 4 : 3;
|
||||
|
||||
pptr = malloc(image->width * ch);
|
||||
pptr = wmalloc(image->width * ch);
|
||||
if (!pptr) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return False;
|
||||
@@ -138,7 +140,7 @@ int RBlurImage(RImage * image)
|
||||
}
|
||||
}
|
||||
|
||||
free(tmpp);
|
||||
wfree(tmpp);
|
||||
|
||||
return True;
|
||||
}
|
||||
|
||||
18
wrlib/load.c
18
wrlib/load.c
@@ -34,6 +34,8 @@
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -125,7 +127,7 @@ static void init_cache(void)
|
||||
RImageCacheMaxImage = IMAGE_CACHE_MAXIMUM_MAXPIXELS;
|
||||
|
||||
if (RImageCacheSize > 0) {
|
||||
RImageCache = malloc(sizeof(RCachedImage) * RImageCacheSize);
|
||||
RImageCache = wmalloc(sizeof(RCachedImage) * RImageCacheSize);
|
||||
if (RImageCache == NULL) {
|
||||
fprintf(stderr, _("wrlib: out of memory for image cache\n"));
|
||||
return;
|
||||
@@ -142,10 +144,10 @@ void RReleaseCache(void)
|
||||
for (i = 0; i < RImageCacheSize; i++) {
|
||||
if (RImageCache[i].file) {
|
||||
RReleaseImage(RImageCache[i].image);
|
||||
free(RImageCache[i].file);
|
||||
wfree(RImageCache[i].file);
|
||||
}
|
||||
}
|
||||
free(RImageCache);
|
||||
wfree(RImageCache);
|
||||
RImageCache = NULL;
|
||||
RImageCacheSize = -1;
|
||||
}
|
||||
@@ -173,7 +175,7 @@ RImage *RLoadImage(RContext *context, const char *file, int index)
|
||||
return RCloneImage(RImageCache[i].image);
|
||||
|
||||
} else {
|
||||
free(RImageCache[i].file);
|
||||
wfree(RImageCache[i].file);
|
||||
RImageCache[i].file = NULL;
|
||||
RReleaseImage(RImageCache[i].image);
|
||||
}
|
||||
@@ -254,8 +256,7 @@ RImage *RLoadImage(RContext *context, const char *file, int index)
|
||||
|
||||
for (i = 0; i < RImageCacheSize; i++) {
|
||||
if (!RImageCache[i].file) {
|
||||
RImageCache[i].file = malloc(strlen(file) + 1);
|
||||
strcpy(RImageCache[i].file, file);
|
||||
RImageCache[i].file = wstrdup(file);
|
||||
RImageCache[i].image = RCloneImage(image);
|
||||
RImageCache[i].last_modif = st.st_mtime;
|
||||
RImageCache[i].last_use = time(NULL);
|
||||
@@ -271,10 +272,9 @@ RImage *RLoadImage(RContext *context, const char *file, int index)
|
||||
|
||||
/* if no slot available, dump least recently used one */
|
||||
if (!done) {
|
||||
free(RImageCache[oldest_idx].file);
|
||||
wfree(RImageCache[oldest_idx].file);
|
||||
RReleaseImage(RImageCache[oldest_idx].image);
|
||||
RImageCache[oldest_idx].file = malloc(strlen(file) + 1);
|
||||
strcpy(RImageCache[oldest_idx].file, file);
|
||||
RImageCache[oldest_idx].file = wstrdup(file);
|
||||
RImageCache[oldest_idx].image = RCloneImage(image);
|
||||
RImageCache[oldest_idx].last_modif = st.st_mtime;
|
||||
RImageCache[oldest_idx].last_use = time(NULL);
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
|
||||
#include <gif_lib.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -127,7 +129,7 @@ RImage *RLoadGIF(const char *file, int index)
|
||||
}
|
||||
}
|
||||
|
||||
buffer = malloc(width * sizeof(GifPixelType));
|
||||
buffer = wmalloc(width * sizeof(GifPixelType));
|
||||
if (!buffer) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
goto bye;
|
||||
@@ -219,7 +221,7 @@ RImage *RLoadGIF(const char *file, int index)
|
||||
did_not_get_any_errors:
|
||||
|
||||
if (buffer)
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
|
||||
if (gif)
|
||||
#if (USE_GIF == 5) && (GIFLIB_MINOR >= 1)
|
||||
|
||||
@@ -35,6 +35,8 @@
|
||||
#include <stdnoreturn.h>
|
||||
#endif
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -119,7 +121,7 @@ static RImage *do_read_jpeg_file(struct jpeg_decompress_struct *cinfo, const cha
|
||||
goto abort_and_release_resources;
|
||||
}
|
||||
|
||||
buffer[0] = (JSAMPROW) malloc(cinfo->image_width * cinfo->num_components);
|
||||
buffer[0] = (JSAMPROW) wmalloc(cinfo->image_width * cinfo->num_components);
|
||||
if (!buffer[0]) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
goto abort_and_release_resources;
|
||||
@@ -167,7 +169,7 @@ static RImage *do_read_jpeg_file(struct jpeg_decompress_struct *cinfo, const cha
|
||||
jpeg_destroy_decompress(cinfo);
|
||||
fclose(file);
|
||||
if (buffer[0])
|
||||
free(buffer[0]);
|
||||
wfree(buffer[0]);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
|
||||
#include <png.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -165,7 +167,7 @@ RImage *RLoadPNG(RContext *context, const char *file)
|
||||
image->background.blue = bkcolor->blue >> 8;
|
||||
}
|
||||
|
||||
png_rows = calloc(height, sizeof(png_bytep));
|
||||
png_rows = wmalloc(height * sizeof(png_bytep));
|
||||
if (!png_rows) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
fclose(f);
|
||||
@@ -174,7 +176,7 @@ RImage *RLoadPNG(RContext *context, const char *file)
|
||||
return NULL;
|
||||
}
|
||||
for (y = 0; y < height; y++) {
|
||||
png_rows[y] = malloc(png_get_rowbytes(png, pinfo));
|
||||
png_rows[y] = wmalloc(png_get_rowbytes(png, pinfo));
|
||||
if (!png_rows[y]) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
fclose(f);
|
||||
@@ -182,8 +184,8 @@ RImage *RLoadPNG(RContext *context, const char *file)
|
||||
png_destroy_read_struct(&png, &pinfo, &einfo);
|
||||
while (y-- > 0)
|
||||
if (png_rows[y])
|
||||
free(png_rows[y]);
|
||||
free(png_rows);
|
||||
wfree(png_rows[y]);
|
||||
wfree(png_rows);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -214,7 +216,7 @@ RImage *RLoadPNG(RContext *context, const char *file)
|
||||
}
|
||||
for (y = 0; y < height; y++)
|
||||
if (png_rows[y])
|
||||
free(png_rows[y]);
|
||||
free(png_rows);
|
||||
wfree(png_rows[y]);
|
||||
wfree(png_rows);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -161,7 +163,7 @@ static RImage *load_graymap(FILE *file, int w, int h, int max, int raw, const ch
|
||||
if (raw == '5') {
|
||||
char *buf;
|
||||
|
||||
buf = malloc(w + 1);
|
||||
buf = wmalloc(w + 1);
|
||||
if (!buf) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
RReleaseImage(image);
|
||||
@@ -169,7 +171,7 @@ static RImage *load_graymap(FILE *file, int w, int h, int max, int raw, const ch
|
||||
}
|
||||
for (y = 0; y < h; y++) {
|
||||
if (!fread(buf, w, 1, file)) {
|
||||
free(buf);
|
||||
wfree(buf);
|
||||
RErrorCode = RERR_BADIMAGEFILE;
|
||||
RReleaseImage(image);
|
||||
return NULL;
|
||||
@@ -181,7 +183,7 @@ static RImage *load_graymap(FILE *file, int w, int h, int max, int raw, const ch
|
||||
*(ptr++) = buf[x];
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
wfree(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
|
||||
#include <webp/decode.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -110,7 +112,7 @@ RImage *RLoadWEBP(const char *file_name)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
raw_data = (uint8_t *) malloc(raw_data_size);
|
||||
raw_data = (uint8_t *) wmalloc(raw_data_size);
|
||||
|
||||
if (!raw_data) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
@@ -124,7 +126,7 @@ RImage *RLoadWEBP(const char *file_name)
|
||||
|
||||
if (r != raw_data_size) {
|
||||
RErrorCode = RERR_READ;
|
||||
free(raw_data);
|
||||
wfree(raw_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -133,7 +135,7 @@ RImage *RLoadWEBP(const char *file_name)
|
||||
fprintf(stderr, _("wrlib: could not get features from WebP file \"%s\", %s\n"),
|
||||
file_name, webp_message_from_status(status));
|
||||
RErrorCode = RERR_BADIMAGEFILE;
|
||||
free(raw_data);
|
||||
wfree(raw_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -141,7 +143,7 @@ RImage *RLoadWEBP(const char *file_name)
|
||||
image = RCreateImage(features.width, features.height, True);
|
||||
if (!image) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
free(raw_data);
|
||||
wfree(raw_data);
|
||||
return NULL;
|
||||
}
|
||||
ret = WebPDecodeRGBAInto(raw_data, raw_data_size, image->data,
|
||||
@@ -151,7 +153,7 @@ RImage *RLoadWEBP(const char *file_name)
|
||||
image = RCreateImage(features.width, features.height, False);
|
||||
if (!image) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
free(raw_data);
|
||||
wfree(raw_data);
|
||||
return NULL;
|
||||
}
|
||||
ret = WebPDecodeRGBInto(raw_data, raw_data_size, image->data,
|
||||
@@ -159,7 +161,7 @@ RImage *RLoadWEBP(const char *file_name)
|
||||
features.width * 3);
|
||||
}
|
||||
|
||||
free(raw_data);
|
||||
wfree(raw_data);
|
||||
|
||||
if (!ret) {
|
||||
fprintf(stderr, _("wrlib: failed to decode WebP from file \"%s\"\n"), file_name);
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
#include <string.h>
|
||||
#include <X11/xpm.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -59,11 +61,11 @@ static RImage *create_rimage_from_xpm(RContext *context, XpmImage xpm)
|
||||
|
||||
/* make color table */
|
||||
for (i = 0; i < 4; i++) {
|
||||
color_table[i] = malloc(xpm.ncolors * sizeof(unsigned char));
|
||||
color_table[i] = wmalloc(xpm.ncolors * sizeof(unsigned char));
|
||||
if (!color_table[i]) {
|
||||
for (i = i - 1; i >= 0; i--) {
|
||||
if (color_table[i])
|
||||
free(color_table[i]);
|
||||
wfree(color_table[i]);
|
||||
}
|
||||
RReleaseImage(image);
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
@@ -123,7 +125,7 @@ static RImage *create_rimage_from_xpm(RContext *context, XpmImage xpm)
|
||||
*(data++) = color_table[3][*p];
|
||||
}
|
||||
for (i = 0; i < 4; i++)
|
||||
free(color_table[i]);
|
||||
wfree(color_table[i]);
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,15 +59,15 @@ static void free_color_symbol_table(unsigned char *color_table[],
|
||||
unsigned short *symbol_table)
|
||||
{
|
||||
if (color_table[0])
|
||||
free(color_table[0]);
|
||||
wfree(color_table[0]);
|
||||
if (color_table[1])
|
||||
free(color_table[1]);
|
||||
wfree(color_table[1]);
|
||||
if (color_table[2])
|
||||
free(color_table[2]);
|
||||
wfree(color_table[2]);
|
||||
if (color_table[3])
|
||||
free(color_table[3]);
|
||||
wfree(color_table[3]);
|
||||
if (symbol_table)
|
||||
free(symbol_table);
|
||||
wfree(symbol_table);
|
||||
}
|
||||
|
||||
RImage *RGetImageFromXPMData(RContext * context, char **data)
|
||||
@@ -95,11 +95,11 @@ RImage *RGetImageFromXPMData(RContext * context, char **data)
|
||||
if (csize != 1 && csize != 2)
|
||||
goto bad_format;
|
||||
|
||||
color_table[0] = malloc(ccount);
|
||||
color_table[1] = malloc(ccount);
|
||||
color_table[2] = malloc(ccount);
|
||||
color_table[3] = malloc(ccount);
|
||||
symbol_table = malloc(ccount * sizeof(unsigned short));
|
||||
color_table[0] = wmalloc(ccount);
|
||||
color_table[1] = wmalloc(ccount);
|
||||
color_table[2] = wmalloc(ccount);
|
||||
color_table[3] = wmalloc(ccount);
|
||||
symbol_table = wmalloc(ccount * sizeof(unsigned short));
|
||||
|
||||
bsize = csize * w + 16;
|
||||
|
||||
@@ -283,14 +283,14 @@ RImage *RLoadXPM(RContext * context, const char *file)
|
||||
if (csize != 1 && csize != 2)
|
||||
goto bad_format;
|
||||
|
||||
color_table[0] = malloc(ccount);
|
||||
color_table[1] = malloc(ccount);
|
||||
color_table[2] = malloc(ccount);
|
||||
color_table[3] = malloc(ccount);
|
||||
symbol_table = malloc(ccount * sizeof(unsigned short));
|
||||
color_table[0] = wmalloc(ccount);
|
||||
color_table[1] = wmalloc(ccount);
|
||||
color_table[2] = wmalloc(ccount);
|
||||
color_table[3] = wmalloc(ccount);
|
||||
symbol_table = wmalloc(ccount * sizeof(unsigned short));
|
||||
|
||||
bsize = csize * w + 16;
|
||||
buffer = malloc(bsize);
|
||||
buffer = wmalloc(bsize);
|
||||
|
||||
if (!color_table[0] || !color_table[1] || !color_table[2] ||
|
||||
!color_table[3] || !symbol_table || !bsize || !buffer) {
|
||||
@@ -298,7 +298,7 @@ RImage *RLoadXPM(RContext * context, const char *file)
|
||||
fclose(f);
|
||||
free_color_symbol_table(color_table, symbol_table);
|
||||
if (buffer)
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -355,7 +355,7 @@ RImage *RLoadXPM(RContext * context, const char *file)
|
||||
fclose(f);
|
||||
free_color_symbol_table(color_table, symbol_table);
|
||||
if (buffer)
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -434,7 +434,7 @@ RImage *RLoadXPM(RContext * context, const char *file)
|
||||
fclose(f);
|
||||
free_color_symbol_table(color_table, symbol_table);
|
||||
if (buffer)
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
return image;
|
||||
|
||||
bad_format:
|
||||
@@ -442,7 +442,7 @@ RImage *RLoadXPM(RContext * context, const char *file)
|
||||
fclose(f);
|
||||
free_color_symbol_table(color_table, symbol_table);
|
||||
if (buffer)
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
if (image)
|
||||
RReleaseImage(image);
|
||||
return NULL;
|
||||
@@ -452,7 +452,7 @@ RImage *RLoadXPM(RContext * context, const char *file)
|
||||
fclose(f);
|
||||
free_color_symbol_table(color_table, symbol_table);
|
||||
if (buffer)
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
if (image)
|
||||
RReleaseImage(image);
|
||||
return NULL;
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#include <string.h>
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "wr_i18n.h"
|
||||
|
||||
@@ -53,7 +55,7 @@ RImage *RCreateImage(unsigned width, unsigned height, int alpha)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
image = malloc(sizeof(RImage));
|
||||
image = wmalloc(sizeof(RImage));
|
||||
if (!image) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return NULL;
|
||||
@@ -68,10 +70,10 @@ RImage *RCreateImage(unsigned width, unsigned height, int alpha)
|
||||
/* the +4 is to give extra bytes at the end of the buffer,
|
||||
* so that we can optimize image conversion for MMX(tm).. see convert.c
|
||||
*/
|
||||
image->data = malloc(width * height * (alpha ? 4 : 3) + 4);
|
||||
image->data = wmalloc(width * height * (alpha ? 4 : 3) + 4);
|
||||
if (!image->data) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
free(image);
|
||||
wfree(image);
|
||||
image = NULL;
|
||||
}
|
||||
|
||||
@@ -94,8 +96,8 @@ void RReleaseImage(RImage * image)
|
||||
image->refCount--;
|
||||
|
||||
if (image->refCount < 1) {
|
||||
free(image->data);
|
||||
free(image);
|
||||
wfree(image->data);
|
||||
wfree(image);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
#include <errno.h>
|
||||
#include <jpeglib.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -59,7 +61,7 @@ Bool RSaveJPEG(RImage *img, const char *filename, char *title)
|
||||
img_depth = 3;
|
||||
|
||||
/* collect separate RGB values to a buffer */
|
||||
buffer = malloc(sizeof(char) * 3 * img->width * img->height);
|
||||
buffer = wmalloc(sizeof(char) * 3 * img->width * img->height);
|
||||
for (y = 0; y < img->height; y++) {
|
||||
for (x = 0; x < img->width; x++) {
|
||||
RGetPixel(img, x, y, &pixel);
|
||||
@@ -97,7 +99,7 @@ Bool RSaveJPEG(RImage *img, const char *filename, char *title)
|
||||
jpeg_finish_compress(&cinfo);
|
||||
|
||||
/* Clean */
|
||||
free(buffer);
|
||||
wfree(buffer);
|
||||
fclose(file);
|
||||
|
||||
return True;
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
#include <errno.h>
|
||||
#include <png.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -95,7 +97,7 @@ Bool RSavePNG(RImage *img, const char *filename, char *title)
|
||||
png_write_info(png_ptr, png_info_ptr);
|
||||
|
||||
/* Allocate memory for one row (3 bytes per pixel - RGB) */
|
||||
png_row = (png_bytep) malloc(3 * width * sizeof(png_byte));
|
||||
png_row = (png_bytep) wmalloc(3 * width * sizeof(png_byte));
|
||||
|
||||
/* Write image data */
|
||||
for (y = 0; y < height; y++) {
|
||||
@@ -121,7 +123,7 @@ Bool RSavePNG(RImage *img, const char *filename, char *title)
|
||||
if (png_ptr != NULL)
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
|
||||
if (png_row != NULL)
|
||||
free(png_row);
|
||||
wfree(png_row);
|
||||
|
||||
return True;
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "imgformat.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -95,7 +97,7 @@ static Bool addcolor(XPMColor ** list, unsigned r, unsigned g, unsigned b, int *
|
||||
if (tmpc)
|
||||
return True;
|
||||
|
||||
newc = malloc(sizeof(XPMColor));
|
||||
newc = wmalloc(sizeof(XPMColor));
|
||||
|
||||
if (!newc) {
|
||||
|
||||
@@ -149,7 +151,7 @@ static void freecolormap(XPMColor * colormap)
|
||||
|
||||
while (colormap) {
|
||||
tmp = colormap->next;
|
||||
free(colormap);
|
||||
wfree(colormap);
|
||||
colormap = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#include "wraster.h"
|
||||
#include "scale.h"
|
||||
#include "wr_i18n.h"
|
||||
@@ -295,7 +297,7 @@ typedef struct {
|
||||
/* clamp the input to the specified range */
|
||||
#define CLAMP(v,l,h) ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
|
||||
|
||||
/* return of calloc is not checked if NULL in the function below! */
|
||||
/* return of wmalloc is not checked if NULL in the function below! */
|
||||
RImage *RSmoothScaleImage(RImage * src, unsigned new_width, unsigned new_height)
|
||||
{
|
||||
CLIST *contrib; /* array of contribution lists */
|
||||
@@ -319,13 +321,13 @@ RImage *RSmoothScaleImage(RImage * src, unsigned new_width, unsigned new_height)
|
||||
yscale = (double)new_height / (double)src->height;
|
||||
|
||||
/* pre-calculate filter contributions for a row */
|
||||
contrib = (CLIST *) calloc(new_width, sizeof(CLIST));
|
||||
contrib = (CLIST *) wmalloc(new_width * sizeof(CLIST));
|
||||
if (xscale < 1.0) {
|
||||
width = fwidth / xscale;
|
||||
fscale = 1.0 / xscale;
|
||||
for (i = 0; i < new_width; ++i) {
|
||||
contrib[i].n = 0;
|
||||
contrib[i].p = (CONTRIB *) calloc((int) ceil(width * 2 + 1), sizeof(CONTRIB));
|
||||
contrib[i].p = (CONTRIB *) wmalloc(ceil(width * 2 + 1) * sizeof(CONTRIB));
|
||||
center = (double)i / xscale;
|
||||
left = ceil(center - width);
|
||||
right = floor(center + width);
|
||||
@@ -348,7 +350,7 @@ RImage *RSmoothScaleImage(RImage * src, unsigned new_width, unsigned new_height)
|
||||
|
||||
for (i = 0; i < new_width; ++i) {
|
||||
contrib[i].n = 0;
|
||||
contrib[i].p = (CONTRIB *) calloc((int) ceil(fwidth * 2 + 1), sizeof(CONTRIB));
|
||||
contrib[i].p = (CONTRIB *) wmalloc(ceil(fwidth * 2 + 1) * sizeof(CONTRIB));
|
||||
center = (double)i / xscale;
|
||||
left = ceil(center - fwidth);
|
||||
right = floor(center + fwidth);
|
||||
@@ -395,18 +397,18 @@ RImage *RSmoothScaleImage(RImage * src, unsigned new_width, unsigned new_height)
|
||||
|
||||
/* free the memory allocated for horizontal filter weights */
|
||||
for (i = 0; i < new_width; ++i) {
|
||||
free(contrib[i].p);
|
||||
wfree(contrib[i].p);
|
||||
}
|
||||
free(contrib);
|
||||
wfree(contrib);
|
||||
|
||||
/* pre-calculate filter contributions for a column */
|
||||
contrib = (CLIST *) calloc(dst->height, sizeof(CLIST));
|
||||
contrib = (CLIST *) wmalloc(dst->height * sizeof(CLIST));
|
||||
if (yscale < 1.0) {
|
||||
width = fwidth / yscale;
|
||||
fscale = 1.0 / yscale;
|
||||
for (i = 0; i < dst->height; ++i) {
|
||||
contrib[i].n = 0;
|
||||
contrib[i].p = (CONTRIB *) calloc((int) ceil(width * 2 + 1), sizeof(CONTRIB));
|
||||
contrib[i].p = (CONTRIB *) wmalloc(ceil(width * 2 + 1) * sizeof(CONTRIB));
|
||||
center = (double)i / yscale;
|
||||
left = ceil(center - width);
|
||||
right = floor(center + width);
|
||||
@@ -428,7 +430,7 @@ RImage *RSmoothScaleImage(RImage * src, unsigned new_width, unsigned new_height)
|
||||
} else {
|
||||
for (i = 0; i < dst->height; ++i) {
|
||||
contrib[i].n = 0;
|
||||
contrib[i].p = (CONTRIB *) calloc((int) ceil(fwidth * 2 + 1), sizeof(CONTRIB));
|
||||
contrib[i].p = (CONTRIB *) wmalloc(ceil(fwidth * 2 + 1) * sizeof(CONTRIB));
|
||||
center = (double)i / yscale;
|
||||
left = ceil(center - fwidth);
|
||||
right = floor(center + fwidth);
|
||||
@@ -450,7 +452,7 @@ RImage *RSmoothScaleImage(RImage * src, unsigned new_width, unsigned new_height)
|
||||
}
|
||||
|
||||
/* apply filter to zoom vertically from tmp to dst */
|
||||
sp = malloc(tmp->height * 3);
|
||||
sp = wmalloc(tmp->height * 3);
|
||||
|
||||
for (k = 0; k < new_width; ++k) {
|
||||
CONTRIB *pp;
|
||||
@@ -485,13 +487,13 @@ RImage *RSmoothScaleImage(RImage * src, unsigned new_width, unsigned new_height)
|
||||
p += new_width * 3;
|
||||
}
|
||||
}
|
||||
free(sp);
|
||||
wfree(sp);
|
||||
|
||||
/* free the memory allocated for vertical filter weights */
|
||||
for (i = 0; i < dst->height; ++i) {
|
||||
free(contrib[i].p);
|
||||
wfree(contrib[i].p);
|
||||
}
|
||||
free(contrib);
|
||||
wfree(contrib);
|
||||
|
||||
RReleaseImage(tmp);
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ int main(int argc, char **argv)
|
||||
else
|
||||
ProgName++;
|
||||
|
||||
color_name = (char **)malloc(sizeof(char *) * argc);
|
||||
color_name = (char **)wmalloc(sizeof(char *) * argc);
|
||||
if (color_name == NULL) {
|
||||
fprintf(stderr, "Cannot allocate memory!\n");
|
||||
exit(1);
|
||||
@@ -106,13 +106,13 @@ int main(int argc, char **argv)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
colors = malloc(sizeof(RColor *) * (ncolors + 1));
|
||||
colors = wmalloc(sizeof(RColor *) * (ncolors + 1));
|
||||
for (i = 0; i < ncolors; i++) {
|
||||
if (!XParseColor(dpy, ctx->cmap, color_name[i], &color)) {
|
||||
printf("could not parse color \"%s\"\n", color_name[i]);
|
||||
exit(1);
|
||||
} else {
|
||||
colors[i] = malloc(sizeof(RColor));
|
||||
colors[i] = wmalloc(sizeof(RColor));
|
||||
colors[i]->red = color.red >> 8;
|
||||
colors[i]->green = color.green >> 8;
|
||||
colors[i]->blue = color.blue >> 8;
|
||||
@@ -149,10 +149,10 @@ int main(int argc, char **argv)
|
||||
|
||||
getchar();
|
||||
|
||||
free(color_name);
|
||||
wfree(color_name);
|
||||
for (i = 0; i < ncolors + 1; i++)
|
||||
free(colors[i]);
|
||||
free(colors);
|
||||
wfree(colors[i]);
|
||||
wfree(colors);
|
||||
|
||||
RDestroyContext(ctx);
|
||||
RShutdown();
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <WINGs/WUtil.h>
|
||||
|
||||
#ifdef USE_XSHM
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
@@ -63,7 +65,7 @@ RXImage *RCreateXImage(RContext * context, int depth, unsigned width, unsigned h
|
||||
RXImage *rximg;
|
||||
Visual *visual = context->visual;
|
||||
|
||||
rximg = malloc(sizeof(RXImage));
|
||||
rximg = wmalloc(sizeof(RXImage));
|
||||
if (!rximg) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return NULL;
|
||||
@@ -71,14 +73,14 @@ RXImage *RCreateXImage(RContext * context, int depth, unsigned width, unsigned h
|
||||
#ifndef USE_XSHM
|
||||
rximg->image = XCreateImage(context->dpy, visual, depth, ZPixmap, 0, NULL, width, height, 8, 0);
|
||||
if (!rximg->image) {
|
||||
free(rximg);
|
||||
wfree(rximg);
|
||||
RErrorCode = RERR_XERROR;
|
||||
return NULL;
|
||||
}
|
||||
rximg->image->data = malloc(rximg->image->bytes_per_line * height);
|
||||
rximg->image->data = wmalloc(rximg->image->bytes_per_line * height);
|
||||
if (!rximg->image->data) {
|
||||
XDestroyImage(rximg->image);
|
||||
free(rximg);
|
||||
wfree(rximg);
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return NULL;
|
||||
}
|
||||
@@ -90,14 +92,14 @@ RXImage *RCreateXImage(RContext * context, int depth, unsigned width, unsigned h
|
||||
rximg->is_shared = 0;
|
||||
rximg->image = XCreateImage(context->dpy, visual, depth, ZPixmap, 0, NULL, width, height, 8, 0);
|
||||
if (!rximg->image) {
|
||||
free(rximg);
|
||||
wfree(rximg);
|
||||
RErrorCode = RERR_XERROR;
|
||||
return NULL;
|
||||
}
|
||||
rximg->image->data = malloc(rximg->image->bytes_per_line * height);
|
||||
rximg->image->data = wmalloc(rximg->image->bytes_per_line * height);
|
||||
if (!rximg->image->data) {
|
||||
XDestroyImage(rximg->image);
|
||||
free(rximg);
|
||||
wfree(rximg);
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return NULL;
|
||||
}
|
||||
@@ -173,7 +175,7 @@ void RDestroyXImage(RContext * context, RXImage * rximage)
|
||||
XDestroyImage(rximage->image);
|
||||
}
|
||||
#endif
|
||||
free(rximage);
|
||||
wfree(rximage);
|
||||
}
|
||||
|
||||
static unsigned getDepth(Display * dpy, Drawable d)
|
||||
@@ -205,7 +207,7 @@ RXImage *RGetXImage(RContext * context, Drawable d, int x, int y, unsigned width
|
||||
}
|
||||
}
|
||||
if (!ximg) {
|
||||
ximg = malloc(sizeof(RXImage));
|
||||
ximg = wmalloc(sizeof(RXImage));
|
||||
if (!ximg) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return NULL;
|
||||
@@ -214,7 +216,7 @@ RXImage *RGetXImage(RContext * context, Drawable d, int x, int y, unsigned width
|
||||
ximg->image = XGetImage(context->dpy, d, x, y, width, height, AllPlanes, ZPixmap);
|
||||
}
|
||||
#else /* !USE_XSHM */
|
||||
ximg = malloc(sizeof(RXImage));
|
||||
ximg = wmalloc(sizeof(RXImage));
|
||||
if (!ximg) {
|
||||
RErrorCode = RERR_NOMEMORY;
|
||||
return NULL;
|
||||
@@ -224,7 +226,7 @@ RXImage *RGetXImage(RContext * context, Drawable d, int x, int y, unsigned width
|
||||
#endif /* !USE_XSHM */
|
||||
|
||||
if (ximg->image == NULL) {
|
||||
free(ximg);
|
||||
wfree(ximg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
[package]
|
||||
name = "wutil-rs"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
crate-type = ["staticlib"]
|
||||
|
||||
[dependencies]
|
||||
hashbrown = "0.16.0"
|
||||
libc = "0.2.175"
|
||||
x11 = "2.21.0"
|
||||
@@ -1,21 +0,0 @@
|
||||
AUTOMAKE_OPTIONS =
|
||||
|
||||
RUST_SOURCES = \
|
||||
src/hash_table.rs \
|
||||
src/lib.rs \
|
||||
src/memory.rs
|
||||
|
||||
RUST_EXTRA = \
|
||||
Cargo.lock \
|
||||
Cargo.toml
|
||||
|
||||
target/debug/libwutil_rs.a: $(RUST_SOURCES) $(RUST_EXTRA)
|
||||
$(CARGO) build
|
||||
|
||||
check-local:
|
||||
$(CARGO) test
|
||||
|
||||
clean-local:
|
||||
$(CARGO) clean
|
||||
|
||||
all: target/debug/libwutil_rs.a
|
||||
@@ -1,328 +0,0 @@
|
||||
use hashbrown::hash_map::{self, HashMap};
|
||||
|
||||
use std::{
|
||||
borrow::Borrow,
|
||||
ffi::{CStr, c_void},
|
||||
hash::{Hash, Hasher},
|
||||
mem,
|
||||
};
|
||||
|
||||
pub enum HashTable {
|
||||
PointerKeyed(HashMap<VoidPointer, VoidPointer>),
|
||||
StringKeyed(HashMap<StringPointer, VoidPointer>),
|
||||
}
|
||||
|
||||
impl HashTable {
|
||||
pub fn new_pointer_keyed() -> Self {
|
||||
HashTable::PointerKeyed(HashMap::new())
|
||||
}
|
||||
|
||||
pub fn new_string_keyed() -> Self {
|
||||
HashTable::StringKeyed(HashMap::new())
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
match self {
|
||||
HashTable::PointerKeyed(m) => m.clear(),
|
||||
HashTable::StringKeyed(m) => m.clear(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
HashTable::PointerKeyed(m) => m.len(),
|
||||
HashTable::StringKeyed(m) => m.len(),
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn get(&self, key: *const i8) -> Option<*mut i8> {
|
||||
match self {
|
||||
HashTable::PointerKeyed(m) => {
|
||||
let key = key.cast_mut();
|
||||
m.get(&key).map(|x| x.0)
|
||||
}
|
||||
HashTable::StringKeyed(m) => {
|
||||
let key = StringPointer(key.cast_mut());
|
||||
let v = m.get(&key).map(|x| x.0);
|
||||
mem::forget(key);
|
||||
v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn insert(&mut self, key: *mut i8, data: VoidPointer) -> Option<VoidPointer> {
|
||||
match self {
|
||||
HashTable::PointerKeyed(m) => m.insert(VoidPointer(key), data),
|
||||
HashTable::StringKeyed(m) => m.insert(StringPointer(key), data),
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn remove(&mut self, key: *const i8) {
|
||||
match self {
|
||||
HashTable::PointerKeyed(m) => {
|
||||
let key = key.cast_mut();
|
||||
m.remove(&key);
|
||||
}
|
||||
HashTable::StringKeyed(m) => {
|
||||
let key = StringPointer(key.cast_mut());
|
||||
m.remove(&key);
|
||||
mem::forget(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Hash)]
|
||||
#[repr(transparent)]
|
||||
pub struct VoidPointer(*mut i8);
|
||||
|
||||
impl Drop for VoidPointer {
|
||||
fn drop(&mut self) {
|
||||
unsafe { libc::free(self.0.cast::<c_void>()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Borrow<*mut i8> for VoidPointer {
|
||||
fn borrow(&self) -> &*mut i8 {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[repr(transparent)]
|
||||
pub struct StringPointer(*mut i8);
|
||||
|
||||
impl PartialEq for StringPointer {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self.0.is_null(), other.0.is_null()) {
|
||||
(true, true) => true,
|
||||
(true, false) => false,
|
||||
(false, true) => false,
|
||||
(false, false) => unsafe { CStr::from_ptr(self.0) == CStr::from_ptr(other.0) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for StringPointer {}
|
||||
|
||||
impl Hash for StringPointer {
|
||||
fn hash<H: Hasher>(&self, h: &mut H) {
|
||||
if self.0.is_null() {
|
||||
h.write_usize(0)
|
||||
} else {
|
||||
unsafe { CStr::from_ptr(self.0).hash(h) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for StringPointer {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
libc::free(self.0.cast::<c_void>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Enumerator<'a> {
|
||||
PointerKeyed(hash_map::IterMut<'a, VoidPointer, VoidPointer>),
|
||||
StringKeyed(hash_map::IterMut<'a, StringPointer, VoidPointer>),
|
||||
}
|
||||
|
||||
pub mod ffi {
|
||||
use std::{
|
||||
ffi::{c_int, c_uint, c_void},
|
||||
mem, ptr,
|
||||
};
|
||||
|
||||
use super::{Enumerator, HashTable, StringPointer, VoidPointer};
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
#[allow(non_snake_case)]
|
||||
pub unsafe extern "C" fn WMCreateIdentityHashTable() -> *mut HashTable {
|
||||
Box::leak(Box::new(HashTable::new_pointer_keyed()))
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
#[allow(non_snake_case)]
|
||||
pub unsafe extern "C" fn WMCreateStringHashTable() -> *mut HashTable {
|
||||
Box::leak(Box::new(HashTable::new_string_keyed()))
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
#[allow(non_snake_case)]
|
||||
pub unsafe extern "C" fn WMFreeHashTable(table: *mut HashTable) {
|
||||
if !table.is_null() {
|
||||
let _ = unsafe { Box::from_raw(table) };
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
#[allow(non_snake_case)]
|
||||
pub unsafe extern "C" fn WMResetHashTable(table: *mut HashTable) {
|
||||
if !table.is_null() {
|
||||
unsafe {
|
||||
(*table).clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
#[allow(non_snake_case)]
|
||||
pub unsafe extern "C" fn WMCountHashTable(table: *mut HashTable) -> c_uint {
|
||||
if table.is_null() {
|
||||
0
|
||||
} else {
|
||||
(unsafe { (*table).len() }) as c_uint
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
#[allow(non_snake_case)]
|
||||
pub unsafe extern "C" fn WMHashGet(table: *mut HashTable, key: *const c_void) -> *mut c_void {
|
||||
if table.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
let key = key.cast::<i8>();
|
||||
(unsafe { (*table).get(key) })
|
||||
.map(|v| v.cast::<c_void>())
|
||||
.unwrap_or(ptr::null_mut())
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn WMHashGetItemAndKey(
|
||||
table: *mut HashTable,
|
||||
key: *const c_void,
|
||||
value_dest: *mut *mut c_void,
|
||||
key_dest: *mut *const c_void,
|
||||
) -> c_int {
|
||||
if table.is_null() {
|
||||
return 0;
|
||||
}
|
||||
let table = unsafe { &mut *table };
|
||||
match table {
|
||||
HashTable::PointerKeyed(m) => {
|
||||
let key = VoidPointer(key.cast::<i8>().cast_mut());
|
||||
let result = match m.get_key_value_mut(&key) {
|
||||
Some((k, v)) => {
|
||||
unsafe {
|
||||
*key_dest = k.0.cast::<c_void>();
|
||||
*value_dest = v.0.cast::<c_void>();
|
||||
}
|
||||
1
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
mem::forget(key);
|
||||
result
|
||||
}
|
||||
HashTable::StringKeyed(m) => {
|
||||
let key = StringPointer(key.cast::<i8>().cast_mut());
|
||||
let result = match m.get_key_value_mut(&key) {
|
||||
Some((k, v)) => {
|
||||
unsafe {
|
||||
*key_dest = k.0.cast::<c_void>();
|
||||
*value_dest = v.0.cast::<c_void>();
|
||||
}
|
||||
1
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
mem::forget(key);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
#[allow(non_snake_case)]
|
||||
pub unsafe extern "C" fn WMHashInsert(
|
||||
table: *mut HashTable,
|
||||
key: *mut c_void,
|
||||
data: *mut c_void,
|
||||
) -> *mut c_void {
|
||||
if table.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
match unsafe { (*table).insert(key.cast::<i8>(), VoidPointer(data.cast::<i8>())) } {
|
||||
Some(v) => {
|
||||
let raw = v.0;
|
||||
mem::forget(v);
|
||||
raw.cast::<c_void>()
|
||||
}
|
||||
None => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
#[allow(non_snake_case)]
|
||||
pub unsafe extern "C" fn WMHashRemove(table: *mut HashTable, key: *mut c_void) {
|
||||
if table.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
(*table).remove(key.cast::<i8>());
|
||||
}
|
||||
}
|
||||
|
||||
/// Important note: this may leak memory if you don't pass the enumerator
|
||||
/// back to [`WMFreeHashEnumerator`]. This is a breaking change from the
|
||||
/// original C implementation, which did not require any resource cleanup.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn WMEnumerateHashTable(
|
||||
table: *mut HashTable,
|
||||
) -> *mut Enumerator<'static> {
|
||||
if table.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
let table = unsafe { &mut *table };
|
||||
match table {
|
||||
HashTable::PointerKeyed(m) => {
|
||||
Box::leak(Box::new(Enumerator::PointerKeyed(m.iter_mut())))
|
||||
}
|
||||
HashTable::StringKeyed(m) => Box::leak(Box::new(Enumerator::StringKeyed(m.iter_mut()))),
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn WMFreeHashEnumerator(e: *mut Enumerator<'static>) {
|
||||
if !e.is_null() {
|
||||
let _ = unsafe { Box::from_raw(e) };
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn WMNextHashEnumeratorItem(e: *mut Enumerator<'static>) -> *mut c_void {
|
||||
if e.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
let e = unsafe { &mut *e };
|
||||
match e {
|
||||
Enumerator::PointerKeyed(i) => match i.next() {
|
||||
Some((_, v)) => v.0.cast::<c_void>(),
|
||||
None => ptr::null_mut(),
|
||||
},
|
||||
Enumerator::StringKeyed(i) => match i.next() {
|
||||
Some((_, v)) => v.0.cast::<c_void>(),
|
||||
None => ptr::null_mut(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn WMNextHashEnumeratorKey(e: *mut Enumerator<'static>) -> *mut c_void {
|
||||
if e.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
let e = unsafe { &mut *e };
|
||||
match e {
|
||||
Enumerator::PointerKeyed(i) => match i.next() {
|
||||
Some((k, _)) => k.0.cast::<c_void>(),
|
||||
None => ptr::null_mut(),
|
||||
},
|
||||
Enumerator::StringKeyed(i) => match i.next() {
|
||||
Some((k, _)) => k.0.cast::<c_void>(),
|
||||
None => ptr::null_mut(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
pub mod hash_table;
|
||||
pub mod memory;
|
||||
@@ -1,222 +0,0 @@
|
||||
//! Custom implementations of malloc/free/realloc.
|
||||
//!
|
||||
//! These are intended for use by C functions that need to allocate. Window
|
||||
//! Maker originally provided [`wmalloc`], [`wfree`], and [`wrealloc`] for
|
||||
//! customizable handling of memory exhaustion (to save workspace state before
|
||||
//! aborting) and to allow optional use of the Boehm GC library. It also tracked
|
||||
//! reference counts, via [`wretain`] and [`wrelease`].
|
||||
//!
|
||||
//! If everything gets rewritten in Rust, we won't need this module anymore. For
|
||||
//! now, it helps to move our allocations into Rust so that it is more
|
||||
//! straightforward to store Rust objects in heap memory that was allocated from
|
||||
//! C. (Rust may have stricter requirements for heap-allocated segments than are
|
||||
//! provided by arbitrary C allocators).
|
||||
//!
|
||||
//! TODO: We may want to restore handling of OOM errors. This would require
|
||||
//! installing a customized Rust allocator, which isn't something you can do yet
|
||||
//! in stable Rust. And, unless our rewrite ends up taking up obscenely more
|
||||
//! memory than the baseline Window Maker code, it isn't really necessary in
|
||||
//! this day and age.
|
||||
|
||||
use std::{alloc, mem, ptr::{self, NonNull}};
|
||||
|
||||
/// Tracks the layout and reference count of an allocated chunk of memory.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Header {
|
||||
ptr: NonNull<u8>,
|
||||
layout: alloc::Layout,
|
||||
refcount: u16,
|
||||
}
|
||||
|
||||
impl Header {
|
||||
/// Recovers the `Header` for the allocated memory chunk `b`.
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// Callers must ensure that `b` is a live allocation from [`wmalloc`] or [`wrealloc`].
|
||||
unsafe fn for_alloc_bytes(b: *mut u8) -> *mut Header {
|
||||
unsafe {
|
||||
b.sub(mem::size_of::<Header>())
|
||||
.cast::<Header>()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocates at least `size` bytes and returns a pointer to them.
|
||||
///
|
||||
/// Returns null if `size` is 0.
|
||||
pub fn alloc_bytes(size: usize) -> *mut u8 {
|
||||
if size == 0 {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
let header_layout = match alloc::Layout::from_size_align(mem::size_of::<Header>(), 8) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return ptr::null_mut(),
|
||||
};
|
||||
|
||||
let layout = match alloc::Layout::from_size_align(size, 8) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return ptr::null_mut(),
|
||||
};
|
||||
let (layout, result_offset) = match header_layout.extend(layout) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return ptr::null_mut(),
|
||||
};
|
||||
|
||||
let full_segment = unsafe { alloc::alloc_zeroed(layout) };
|
||||
if full_segment.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
let result = unsafe { full_segment.add(result_offset) };
|
||||
if result.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let header = result.sub(mem::size_of::<Header>()).cast::<Header>();
|
||||
header.write_unaligned(Header {
|
||||
ptr: NonNull::new_unchecked(full_segment),
|
||||
layout: header_layout,
|
||||
refcount: 0,
|
||||
});
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Frees the bytes pointed to by `b`.
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// Callers must ensure that `b` is a live allocation from [`wmalloc`] or [`wrealloc`].
|
||||
pub unsafe fn free_bytes(b: *mut u8) {
|
||||
if b.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
let header = &*Header::for_alloc_bytes(b);
|
||||
alloc::dealloc(header.ptr.as_ptr(), header.layout);
|
||||
}
|
||||
}
|
||||
|
||||
/// Functions to be called from C.
|
||||
pub mod ffi {
|
||||
use super::{alloc_bytes, free_bytes, Header};
|
||||
|
||||
use std::{ffi::c_void, ptr};
|
||||
|
||||
/// Allocates `size` bytes. Returns null if `sizes is 0.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wmalloc(size: usize) -> *mut c_void {
|
||||
alloc_bytes(size).cast::<c_void>()
|
||||
}
|
||||
|
||||
/// Frees `ptr`, which must have come from [`wmalloc`] or [`wrealloc`].
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wfree(ptr: *mut c_void) {
|
||||
unsafe { free_bytes(ptr.cast::<u8>()); }
|
||||
}
|
||||
|
||||
/// Resizes `ptr` to be at least `newsize` bytes in size, returning the
|
||||
/// start of the new segment.
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// Callers must ensure that `ptr` is a live allocation from [`wmalloc`] or [`wrealloc`].
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wrealloc(ptr: *mut c_void, newsize: usize) -> *mut c_void {
|
||||
unsafe {
|
||||
wfree(ptr);
|
||||
wmalloc(newsize).cast::<c_void>()
|
||||
}
|
||||
}
|
||||
|
||||
/// Bumps the refcount for `ptr`.
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// Callers must ensure that `b` is a live allocation from [`wmalloc`] or [`wrealloc`].
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wretain(ptr: *mut c_void) -> *mut c_void {
|
||||
if ptr.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
unsafe {
|
||||
let header = Header::for_alloc_bytes(ptr.cast::<u8>());
|
||||
(*header).refcount += 1;
|
||||
}
|
||||
ptr
|
||||
}
|
||||
|
||||
/// Decrements the refcount for `ptr`. If this brings the refcount to 0,
|
||||
/// frees `ptr`.
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// Callers must ensure that `ptr` is a live allocation from [`wmalloc`] or [`wrealloc`].
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn wrelease(ptr: *mut c_void) {
|
||||
let ptr = ptr.cast::<u8>();
|
||||
if ptr.is_null() {
|
||||
return;
|
||||
}
|
||||
let header = unsafe { &mut *Header::for_alloc_bytes(ptr) };
|
||||
match header.refcount {
|
||||
0 | 1 => unsafe { free_bytes(ptr) },
|
||||
_ => header.refcount -= 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{alloc_bytes, free_bytes, ffi::wrealloc, Header};
|
||||
|
||||
use std::{mem, os::raw::c_void, ptr};
|
||||
|
||||
#[test]
|
||||
fn recover_header() {
|
||||
unsafe {
|
||||
let x = alloc_bytes(mem::size_of::<i64>());
|
||||
let header = Header::for_alloc_bytes(x);
|
||||
assert_eq!(header.cast::<u8>().add(mem::size_of::<Header>()), x);
|
||||
// This may be allocator-dependent, but it's a reasonable sanity check for now.
|
||||
assert!((*header).ptr.as_ptr() <= header.cast::<u8>());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alloc_zero_returns_null() {
|
||||
assert!(alloc_bytes(0).is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn free_null() {
|
||||
unsafe { free_bytes(ptr::null_mut()); }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn realloc_null() {
|
||||
unsafe { assert!(wrealloc(ptr::null_mut(), 0).is_null()); }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alloc_free_nonzero() {
|
||||
let x = alloc_bytes(mem::size_of::<i64>()).cast::<i64>();
|
||||
assert!(!x.is_null());
|
||||
unsafe { *x = 42; }
|
||||
assert_eq!(unsafe { *x }, 42);
|
||||
unsafe { free_bytes(x.cast::<u8>()); }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn realloc_nonzero() {
|
||||
let x = alloc_bytes(mem::size_of::<i64>()).cast::<c_void>();
|
||||
assert!(!x.is_null());
|
||||
let y = unsafe { wrealloc(x, mem::size_of::<i32>()).cast::<i32>() };
|
||||
assert!(!y.is_null());
|
||||
unsafe { *y = 17; }
|
||||
assert_eq!(unsafe { *y }, 17);
|
||||
unsafe { free_bytes(y.cast::<u8>()); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user