Compare commits

..
Author SHA1 Message Date
trurl 4732296762 Port WINGs data.c (WMData) to Rust.
This is not tested super well, but I hope we can be rid of it soon enough. (Once
we have WMPropList migrated, WMData and other WINGs data structures should be
easier to prune.)
2025-10-04 12:31:54 -04:00
trurl 3846a5017c Correct oversight in how WMMapArray is supposed to work. 2025-10-03 22:55:52 -04:00
trurl d736af8321 Drop WMData's destructor field.
WMData always owns its data and allocates it with wmalloc, so we can always free
it with wfree (and don't need to call anything else).
2025-10-03 01:19:07 -04:00
trurl 506e14cd24 Eliminate the WINGs function WMCreateDataWithBytesNoCopy.
This constructor was only needed in one particular place. We can duplidate the
data instead of borrowing it. This ensures that WMData always owns its data
segment, which simplifies porting to Rust significantly.
2025-10-03 01:16:32 -04:00
trurl ee047b63df Eliminate the unused WINGs function WMGetSubdataWithRange. 2025-10-03 01:02:40 -04:00
trurl 0b5033a0e4 Eliminate the unused WINGs function WMCreatePLDataWithBytesNoCopy. 2025-10-03 01:01:15 -04:00
trurl f6c897de95 Forgot to add new array.rs impl. 2025-10-03 00:50:02 -04:00
trurl 249dee3874 Fix const qualifier on strings returned by wgethomedir. 2025-10-03 00:44:58 -04:00
trurl 0d6f5b6d57 Reimplement WINGs array.c in Rust.
This is another utility that should not be used in any new (Rust) code. (We
should prefer Vec or something similar.) This should be removed once dependents
are ported to Rust.
2025-10-03 00:43:56 -04:00
trurl e4c619bb14 Replace most WUtil functions in findfile.c with Rust impls.
This is not a bug-for-bug reimplementation, and it may need some shaking down to
ensure that everything still works. Once their dependents are ported, it would
be appropriate to dispose of them.
2025-10-02 14:26:37 -04:00
trurl adb601eaa9 Use free() on memory returned by FcNameUnparse and hand back wfree-managed pointers from our functions.
This is necessary because we now allocate memory through a special allocator of
our own on the Rust side. Passing raw malloc'd pointers to wfree will break
things.
2025-10-02 13:26:57 -04:00
trurl a07bdb747d Remember to AC_SUBST the Rust compiler environment variables so they're visible in Makefiles. 2025-10-02 13:24:51 -04:00
trurl d7bde34561 Port custom allocators (WINGs memory.c) to Rust.
This introduces the crate wutil-rs, which is intended to be the destination for
migrating the API of WINGs/WINGs/WUtil.h to Rust.
2025-09-21 15:31:47 -04:00
trurl ceeddfb9da Rip out Boehm GC support.
This is done to simplify memory management across the boundary between C and
Rust. While rewriting WINGs, we may want to be able to malloc/free with the libc
allocator on both sides of that divide.
2025-09-19 20:04:10 -04:00
79 changed files with 1474 additions and 1729 deletions
+1 -1
View File
@@ -39,7 +39,7 @@ ACLOCAL_AMFLAGS = -I m4
AM_DISTCHECK_CONFIGURE_FLAGS = --enable-silent-rules LINGUAS='*'
SUBDIRS = wrlib WINGs wmaker-rs src util po WindowMaker wmlib WPrefs.app doc
SUBDIRS = wrlib wutil-rs WINGs wmaker-rs src util po WindowMaker wmlib WPrefs.app doc
DIST_SUBDIRS = $(SUBDIRS) test
EXTRA_DIST = TODO BUGS BUGFORM FAQ INSTALL \
+5 -6
View File
@@ -10,10 +10,12 @@ 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 $(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@
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)
EXTRA_DIST = BUGS make-rgb Examples Extras Tests
@@ -62,15 +64,12 @@ libWINGs_la_SOURCES = \
wwindow.c
libWUtil_la_SOURCES = \
array.c \
bagtree.c \
data.c \
error.c \
error.h \
findfile.c \
handlers.c \
hashtable.c \
memory.c \
menuparser.c \
menuparser.h \
menuparser_macros.c \
+9 -50
View File
@@ -213,10 +213,6 @@ void wfree(void *ptr);
void wrelease(void *ptr);
void* wretain(void *ptr);
typedef void waborthandler(int);
waborthandler* wsetabort(waborthandler* handler);
/* ---[ WINGs/error.c ]--------------------------------------------------- */
enum {
@@ -248,8 +244,8 @@ char* wexpandpath(const char *path);
int wcopy_file(const char *toPath, const char *srcFile, const char *destFile);
/* don't free the returned string */
const char* wgethomedir(void);
/* You must free the returned string! */
char* wgethomedir(void);
/* ---[ WINGs/proplist.c ]------------------------------------------------ */
@@ -400,7 +396,7 @@ extern const WMHashTableCallbacks WMStringPointerHashCallbacks;
/* keys are strings, but they are not copied */
/* ---[ WINGs/array.c ]--------------------------------------------------- */
/* ---[ wutil-rs/src/array.rs ]--------------------------------------------------- */
/*
* WMArray use an array to store the elements.
@@ -422,29 +418,22 @@ WMArray* WMCreateArrayWithDestructor(int initialSize, WMFreeDataProc *destructor
WMArray* WMCreateArrayWithArray(WMArray *array);
#define WMDuplicateArray(array) WMCreateArrayWithArray(array)
void WMEmptyArray(WMArray *array);
void WMFreeArray(WMArray *array);
int WMGetArrayItemCount(WMArray *array);
/* appends other to array. other remains unchanged */
void WMAppendArray(WMArray *array, WMArray *other);
/* add will place the element at the end of the array */
void WMAddToArray(WMArray *array, void *item);
/* insert will increment the index of elements after it by 1 */
void WMInsertInArray(WMArray *array, int index, void *item);
/* replace and set will return the old item WITHOUT calling the
/* set returns the old item WITHOUT calling the
* destructor on it even if its available. Free the returned item yourself.
*/
void* WMReplaceInArray(WMArray *array, int index, void *item);
#define WMSetInArray(array, index, item) WMReplaceInArray(array, index, item)
void* WMSetInArray(WMArray *array, int index, void *item);
/* delete and remove will remove the elements and cause the elements
* after them to decrement their indexes by 1. Also will call the
@@ -452,20 +441,21 @@ void* WMReplaceInArray(WMArray *array, int index, void *item);
*/
int WMDeleteFromArray(WMArray *array, int index);
#define WMRemoveFromArray(array, item) WMRemoveFromArrayMatching(array, NULL, item)
int WMRemoveFromArray(WMArray *array, void *item);
int WMRemoveFromArrayMatching(WMArray *array, WMMatchDataProc *match, void *cdata);
void* WMGetFromArray(WMArray *array, int index);
#define WMGetFirstInArray(array, item) WMFindInArray(array, NULL, item)
/* pop will return the last element from the array, also removing it
* from the array. The destructor is NOT called, even if available.
* Free the returned element if needed by yourself
*/
void* WMPopFromArray(WMArray *array);
/* Like WMFindInArray(array, NULL, item) */
int WMGetFirstInArray(WMArray *array, void *item);
int WMFindInArray(WMArray *array, WMMatchDataProc *match, void *cdata);
int WMCountInArray(WMArray *array, void *item);
@@ -479,8 +469,6 @@ void WMSortArray(WMArray *array, WMCompareDataProc *comparer);
void WMMapArray(WMArray *array, void (*function)(void*, void*), void *data);
WMArray* WMGetSubarrayWithRange(WMArray* array, WMRange aRange);
void* WMArrayFirst(WMArray *array, WMArrayIterator *iter);
void* WMArrayLast(WMArray *array, WMArrayIterator *iter);
@@ -604,37 +592,16 @@ WMData* WMCreateDataWithLength(unsigned length);
WMData* WMCreateDataWithBytes(const void *bytes, unsigned length);
/* destructor is a function called to free the data when releasing the data
* object, or NULL if no freeing of data is necesary. */
WMData* WMCreateDataWithBytesNoCopy(void *bytes, unsigned length,
WMFreeDataProc *destructor);
WMData* WMCreateDataWithData(WMData *aData);
WMData* WMRetainData(WMData *aData);
void WMReleaseData(WMData *aData);
/* Adjusting capacity */
void WMSetDataCapacity(WMData *aData, unsigned capacity);
void WMSetDataLength(WMData *aData, unsigned length);
void WMIncreaseDataLengthBy(WMData *aData, unsigned extraLength);
/* Accessing data */
const void* WMDataBytes(WMData *aData);
void WMGetDataBytes(WMData *aData, void *buffer);
void WMGetDataBytesWithLength(WMData *aData, void *buffer, unsigned length);
void WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange);
WMData* WMGetSubdataWithRange(WMData *aData, WMRange aRange);
/* Testing data */
Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
@@ -649,10 +616,6 @@ void WMAppendData(WMData *aData, WMData *anotherData);
/* Modifying data */
void WMReplaceDataBytesInRange(WMData *aData, WMRange aRange, const void *bytes);
void WMResetDataBytesInRange(WMData *aData, WMRange aRange);
void WMSetData(WMData *aData, WMData *anotherData);
@@ -768,10 +731,6 @@ WMPropList* WMCreatePLData(WMData *data);
WMPropList* WMCreatePLDataWithBytes(const unsigned char *bytes, unsigned int length);
WMPropList* WMCreatePLDataWithBytesNoCopy(unsigned char *bytes,
unsigned int length,
WMFreeDataProc *destructor);
WMPropList* WMCreatePLArray(WMPropList *elem, ...);
WMPropList* WMCreatePLDictionary(WMPropList *key, WMPropList *value, ...);
-363
View File
@@ -1,363 +0,0 @@
/*
* Dynamically Resized Array
*
* Authors: Alfredo K. Kojima <kojima@windowmaker.info>
* Dan Pascu <dan@windowmaker.info>
*
* This code is released to the Public Domain, but
* proper credit is always appreciated :)
*/
#include <stdlib.h>
#include <string.h>
#include "WUtil.h"
#define INITIAL_SIZE 8
#define RESIZE_INCREMENT 8
typedef struct W_Array {
void **items; /* the array data */
int itemCount; /* # of items in array */
int allocSize; /* allocated size of array */
WMFreeDataProc *destructor; /* the destructor to free elements */
} W_Array;
WMArray *WMCreateArray(int initialSize)
{
return WMCreateArrayWithDestructor(initialSize, NULL);
}
WMArray *WMCreateArrayWithDestructor(int initialSize, WMFreeDataProc * destructor)
{
WMArray *array;
array = wmalloc(sizeof(WMArray));
if (initialSize <= 0) {
initialSize = INITIAL_SIZE;
}
array->items = wmalloc(sizeof(void *) * initialSize);
array->itemCount = 0;
array->allocSize = initialSize;
array->destructor = destructor;
return array;
}
WMArray *WMCreateArrayWithArray(WMArray * array)
{
WMArray *newArray;
newArray = wmalloc(sizeof(WMArray));
newArray->items = wmalloc(sizeof(void *) * array->allocSize);
memcpy(newArray->items, array->items, sizeof(void *) * array->itemCount);
newArray->itemCount = array->itemCount;
newArray->allocSize = array->allocSize;
newArray->destructor = NULL;
return newArray;
}
void WMEmptyArray(WMArray * array)
{
if (array->destructor) {
while (array->itemCount > 0) {
array->itemCount--;
array->destructor(array->items[array->itemCount]);
}
}
/*memset(array->items, 0, array->itemCount * sizeof(void*)); */
array->itemCount = 0;
}
void WMFreeArray(WMArray * array)
{
if (array == NULL)
return;
WMEmptyArray(array);
wfree(array->items);
wfree(array);
}
int WMGetArrayItemCount(WMArray * array)
{
if (array == NULL)
return 0;
return array->itemCount;
}
void WMAppendArray(WMArray * array, WMArray * other)
{
if (array == NULL || other == NULL)
return;
if (other->itemCount == 0)
return;
if (array->itemCount + other->itemCount > array->allocSize) {
array->allocSize += other->allocSize;
array->items = wrealloc(array->items, sizeof(void *) * array->allocSize);
}
memcpy(array->items + array->itemCount, other->items, sizeof(void *) * other->itemCount);
array->itemCount += other->itemCount;
}
void WMAddToArray(WMArray * array, void *item)
{
if (array == NULL)
return;
if (array->itemCount >= array->allocSize) {
array->allocSize += RESIZE_INCREMENT;
array->items = wrealloc(array->items, sizeof(void *) * array->allocSize);
}
array->items[array->itemCount] = item;
array->itemCount++;
}
void WMInsertInArray(WMArray * array, int index, void *item)
{
if (array == NULL)
return;
wassertr(index >= 0 && index <= array->itemCount);
if (array->itemCount >= array->allocSize) {
array->allocSize += RESIZE_INCREMENT;
array->items = wrealloc(array->items, sizeof(void *) * array->allocSize);
}
if (index < array->itemCount) {
memmove(array->items + index + 1, array->items + index,
sizeof(void *) * (array->itemCount - index));
}
array->items[index] = item;
array->itemCount++;
}
void *WMReplaceInArray(WMArray * array, int index, void *item)
{
void *old;
if (array == NULL)
return NULL;
wassertrv(index >= 0 && index <= array->itemCount, NULL);
/* is it really useful to perform append if index == array->itemCount ? -Dan */
if (index == array->itemCount) {
WMAddToArray(array, item);
return NULL;
}
old = array->items[index];
array->items[index] = item;
return old;
}
int WMDeleteFromArray(WMArray * array, int index)
{
if (array == NULL)
return 0;
wassertrv(index >= 0 && index < array->itemCount, 0);
if (array->destructor) {
array->destructor(array->items[index]);
}
if (index < array->itemCount - 1) {
memmove(array->items + index, array->items + index + 1,
sizeof(void *) * (array->itemCount - index - 1));
}
array->itemCount--;
return 1;
}
int WMRemoveFromArrayMatching(WMArray * array, WMMatchDataProc * match, void *cdata)
{
int i;
if (array == NULL)
return 1;
if (match != NULL) {
for (i = 0; i < array->itemCount; i++) {
if ((*match) (array->items[i], cdata)) {
WMDeleteFromArray(array, i);
return 1;
}
}
} else {
for (i = 0; i < array->itemCount; i++) {
if (array->items[i] == cdata) {
WMDeleteFromArray(array, i);
return 1;
}
}
}
return 0;
}
void *WMGetFromArray(WMArray * array, int index)
{
if (index < 0 || array == NULL || index >= array->itemCount)
return NULL;
return array->items[index];
}
void *WMPopFromArray(WMArray * array)
{
if (array == NULL || array->itemCount <= 0)
return NULL;
array->itemCount--;
return array->items[array->itemCount];
}
int WMFindInArray(WMArray * array, WMMatchDataProc * match, void *cdata)
{
int i;
if (array == NULL)
return WANotFound;
if (match != NULL) {
for (i = 0; i < array->itemCount; i++) {
if ((*match) (array->items[i], cdata))
return i;
}
} else {
for (i = 0; i < array->itemCount; i++) {
if (array->items[i] == cdata)
return i;
}
}
return WANotFound;
}
int WMCountInArray(WMArray * array, void *item)
{
int i, count;
if (array == NULL)
return 0;
for (i = 0, count = 0; i < array->itemCount; i++) {
if (array->items[i] == item)
count++;
}
return count;
}
void WMSortArray(WMArray * array, WMCompareDataProc * comparer)
{
if (array == NULL)
return;
if (array->itemCount > 1) { /* Don't sort empty or single element arrays */
qsort(array->items, array->itemCount, sizeof(void *), comparer);
}
}
void WMMapArray(WMArray * array, void (*function) (void *, void *), void *data)
{
int i;
if (array == NULL)
return;
for (i = 0; i < array->itemCount; i++) {
(*function) (array->items[i], data);
}
}
WMArray *WMGetSubarrayWithRange(WMArray * array, WMRange aRange)
{
WMArray *newArray;
if (aRange.count <= 0 || array == NULL)
return WMCreateArray(0);
if (aRange.position < 0)
aRange.position = 0;
if (aRange.position >= array->itemCount)
aRange.position = array->itemCount - 1;
if (aRange.position + aRange.count > array->itemCount)
aRange.count = array->itemCount - aRange.position;
newArray = WMCreateArray(aRange.count);
memcpy(newArray->items, array->items + aRange.position, sizeof(void *) * aRange.count);
newArray->itemCount = aRange.count;
return newArray;
}
void *WMArrayFirst(WMArray * array, WMArrayIterator * iter)
{
if (array == NULL || array->itemCount == 0) {
*iter = WANotFound;
return NULL;
} else {
*iter = 0;
return array->items[0];
}
}
void *WMArrayLast(WMArray * array, WMArrayIterator * iter)
{
if (array == NULL || array->itemCount == 0) {
*iter = WANotFound;
return NULL;
} else {
*iter = array->itemCount - 1;
return array->items[*iter];
}
}
void *WMArrayNext(WMArray * array, WMArrayIterator * iter)
{
if (array == NULL) {
*iter = WANotFound;
return NULL;
}
if (*iter >= 0 && *iter < array->itemCount - 1) {
return array->items[++(*iter)];
} else {
*iter = WANotFound;
return NULL;
}
}
void *WMArrayPrevious(WMArray * array, WMArrayIterator * iter)
{
if (array == NULL) {
*iter = WANotFound;
return NULL;
}
if (*iter > 0 && *iter < array->itemCount) {
return array->items[--(*iter)];
} else {
*iter = WANotFound;
return NULL;
}
}
-289
View File
@@ -1,289 +0,0 @@
/*
* WINGs WMData function library
*
* Copyright (c) 1999-2003 Dan Pascu
*
* 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 <string.h>
#include "WUtil.h"
typedef struct W_Data {
unsigned length; /* How many bytes we have */
unsigned capacity; /* How many bytes it can hold */
unsigned growth; /* How much to grow */
void *bytes; /* Actual data */
unsigned retainCount;
WMFreeDataProc *destructor;
int format; /* 0, 8, 16 or 32 */
} W_Data;
/* Creating and destroying data objects */
WMData *WMCreateDataWithCapacity(unsigned capacity)
{
WMData *aData;
aData = (WMData *) wmalloc(sizeof(WMData));
if (capacity > 0)
aData->bytes = wmalloc(capacity);
else
aData->bytes = NULL;
aData->capacity = capacity;
aData->growth = capacity / 2 > 0 ? capacity / 2 : 1;
aData->length = 0;
aData->retainCount = 1;
aData->format = 0;
aData->destructor = wfree;
return aData;
}
WMData *WMCreateDataWithLength(unsigned length)
{
WMData *aData;
aData = WMCreateDataWithCapacity(length);
if (length > 0) {
aData->length = length;
}
return aData;
}
WMData *WMCreateDataWithBytes(const void *bytes, unsigned length)
{
WMData *aData;
aData = WMCreateDataWithCapacity(length);
aData->length = length;
memcpy(aData->bytes, bytes, length);
return aData;
}
WMData *WMCreateDataWithBytesNoCopy(void *bytes, unsigned length, WMFreeDataProc * destructor)
{
WMData *aData;
aData = (WMData *) wmalloc(sizeof(WMData));
aData->length = length;
aData->capacity = length;
aData->growth = length / 2 > 0 ? length / 2 : 1;
aData->bytes = bytes;
aData->retainCount = 1;
aData->format = 0;
aData->destructor = destructor;
return aData;
}
WMData *WMCreateDataWithData(WMData * aData)
{
WMData *newData;
if (aData->length > 0) {
newData = WMCreateDataWithBytes(aData->bytes, aData->length);
} else {
newData = WMCreateDataWithCapacity(0);
}
newData->format = aData->format;
return newData;
}
WMData *WMRetainData(WMData * aData)
{
aData->retainCount++;
return aData;
}
void WMReleaseData(WMData * aData)
{
aData->retainCount--;
if (aData->retainCount > 0)
return;
if (aData->bytes != NULL && aData->destructor != NULL) {
aData->destructor(aData->bytes);
}
wfree(aData);
}
/* Adjusting capacity */
void WMSetDataCapacity(WMData * aData, unsigned capacity)
{
if (aData->capacity != capacity) {
aData->bytes = wrealloc(aData->bytes, capacity);
aData->capacity = capacity;
aData->growth = capacity / 2 > 0 ? capacity / 2 : 1;
}
if (aData->length > capacity) {
aData->length = capacity;
}
}
void WMSetDataLength(WMData * aData, unsigned length)
{
if (length > aData->capacity) {
WMSetDataCapacity(aData, length);
}
if (length > aData->length) {
memset((unsigned char *)aData->bytes + aData->length, 0, length - aData->length);
}
aData->length = length;
}
void WMSetDataFormat(WMData * aData, unsigned format)
{
aData->format = format;
}
void WMIncreaseDataLengthBy(WMData * aData, unsigned extraLength)
{
WMSetDataLength(aData, aData->length + extraLength);
}
/* Accessing data */
const void *WMDataBytes(WMData * aData)
{
return aData->bytes;
}
void WMGetDataBytes(WMData * aData, void *buffer)
{
wassertr(aData->length > 0);
memcpy(buffer, aData->bytes, aData->length);
}
unsigned WMGetDataFormat(WMData * aData)
{
return aData->format;
}
void WMGetDataBytesWithLength(WMData * aData, void *buffer, unsigned length)
{
wassertr(aData->length > 0);
wassertr(length <= aData->length);
memcpy(buffer, aData->bytes, length);
}
void WMGetDataBytesWithRange(WMData * aData, void *buffer, WMRange aRange)
{
wassertr(aRange.position < aData->length);
wassertr(aRange.count <= aData->length - aRange.position);
memcpy(buffer, (unsigned char *)aData->bytes + aRange.position, aRange.count);
}
WMData *WMGetSubdataWithRange(WMData * aData, WMRange aRange)
{
void *buffer;
WMData *newData;
if (aRange.count <= 0)
return WMCreateDataWithCapacity(0);
buffer = wmalloc(aRange.count);
WMGetDataBytesWithRange(aData, buffer, aRange);
newData = WMCreateDataWithBytesNoCopy(buffer, aRange.count, wfree);
newData->format = aData->format;
return newData;
}
/* Testing data */
Bool WMIsDataEqualToData(WMData * aData, WMData * anotherData)
{
if (aData->length != anotherData->length)
return False;
else if (!aData->bytes && !anotherData->bytes) /* both are empty */
return True;
else if (!aData->bytes || !anotherData->bytes) /* one of them is empty */
return False;
return (memcmp(aData->bytes, anotherData->bytes, aData->length) == 0);
}
unsigned WMGetDataLength(WMData * aData)
{
return aData->length;
}
/* Adding data */
void WMAppendDataBytes(WMData * aData, const void *bytes, unsigned length)
{
unsigned oldLength = aData->length;
unsigned newLength = oldLength + length;
if (newLength > aData->capacity) {
unsigned nextCapacity = aData->capacity + aData->growth;
unsigned nextGrowth = aData->capacity ? aData->capacity : 1;
while (nextCapacity < newLength) {
unsigned tmp = nextCapacity + nextGrowth;
nextGrowth = nextCapacity;
nextCapacity = tmp;
}
WMSetDataCapacity(aData, nextCapacity);
aData->growth = nextGrowth;
}
memcpy((unsigned char *)aData->bytes + oldLength, bytes, length);
aData->length = newLength;
}
void WMAppendData(WMData * aData, WMData * anotherData)
{
if (anotherData->length > 0)
WMAppendDataBytes(aData, anotherData->bytes, anotherData->length);
}
/* Modifying data */
void WMReplaceDataBytesInRange(WMData * aData, WMRange aRange, const void *bytes)
{
wassertr(aRange.position < aData->length);
wassertr(aRange.count <= aData->length - aRange.position);
memcpy((unsigned char *)aData->bytes + aRange.position, bytes, aRange.count);
}
void WMResetDataBytesInRange(WMData * aData, WMRange aRange)
{
wassertr(aRange.position < aData->length);
wassertr(aRange.count <= aData->length - aRange.position);
memset((unsigned char *)aData->bytes + aRange.position, 0, aRange.count);
}
void WMSetData(WMData * aData, WMData * anotherData)
{
unsigned length = anotherData->length;
WMSetDataCapacity(aData, length);
if (length > 0)
memcpy(aData->bytes, anotherData->bytes, length);
aData->length = length;
}
/* Storing data */
-427
View File
@@ -34,331 +34,6 @@
#include <pwd.h>
#include <limits.h>
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
const char *wgethomedir(void)
{
static char *home = NULL;
char *tmp;
struct passwd *user;
if (home)
return home;
tmp = GETENV("HOME");
if (tmp) {
home = wstrdup(tmp);
return home;
}
user = getpwuid(getuid());
if (!user) {
werror(_("could not get password entry for UID %i"), getuid());
home = "/";
return home;
}
if (!user->pw_dir)
home = "/";
else
home = wstrdup(user->pw_dir);
return home;
}
/*
* Return the home directory for the specified used
*
* If user not found, returns NULL, otherwise always returns a path that is
* statically stored.
*
* Please note you must use the path before any other call to 'getpw*' or it
* may be erased. This is a design choice to avoid duplication considering
* the use case for this function.
*/
static const char *getuserhomedir(const char *username)
{
static const char default_home[] = "/";
struct passwd *user;
user = getpwnam(username);
if (!user) {
werror(_("could not get password entry for user %s"), username);
return NULL;
}
if (!user->pw_dir)
return default_home;
else
return user->pw_dir;
}
char *wexpandpath(const char *path)
{
const char *origpath = path;
char buffer2[PATH_MAX + 2];
char buffer[PATH_MAX + 2];
int i;
memset(buffer, 0, PATH_MAX + 2);
if (*path == '~') {
const char *home;
path++;
if (*path == '/' || *path == 0) {
home = wgethomedir();
if (strlen(home) > PATH_MAX ||
wstrlcpy(buffer, home, sizeof(buffer)) >= sizeof(buffer))
goto error;
} else {
int j;
j = 0;
while (*path != 0 && *path != '/') {
if (j > PATH_MAX)
goto error;
buffer2[j++] = *path;
buffer2[j] = 0;
path++;
}
home = getuserhomedir(buffer2);
if (!home || wstrlcat(buffer, home, sizeof(buffer)) >= sizeof(buffer))
goto error;
}
}
i = strlen(buffer);
while (*path != 0 && i <= PATH_MAX) {
char *tmp;
if (*path == '$') {
int j;
path++;
/* expand $(HOME) or $HOME style environment variables */
if (*path == '(') {
path++;
j = 0;
while (*path != 0 && *path != ')') {
if (j > PATH_MAX)
goto error;
buffer2[j++] = *(path++);
}
buffer2[j] = 0;
if (*path == ')') {
path++;
tmp = getenv(buffer2);
} else {
tmp = NULL;
}
if (!tmp) {
if ((i += strlen(buffer2) + 2) > PATH_MAX)
goto error;
buffer[i] = 0;
if (wstrlcat(buffer, "$(", sizeof(buffer)) >= sizeof(buffer) ||
wstrlcat(buffer, buffer2, sizeof(buffer)) >= sizeof(buffer))
goto error;
if (*(path-1)==')') {
if (++i > PATH_MAX ||
wstrlcat(buffer, ")", sizeof(buffer)) >= sizeof(buffer))
goto error;
}
} else {
if ((i += strlen(tmp)) > PATH_MAX ||
wstrlcat(buffer, tmp, sizeof(buffer)) >= sizeof(buffer))
goto error;
}
} else {
j = 0;
while (*path != 0 && *path != '/') {
if (j > PATH_MAX)
goto error;
buffer2[j++] = *(path++);
}
buffer2[j] = 0;
tmp = getenv(buffer2);
if (!tmp) {
if ((i += strlen(buffer2) + 1) > PATH_MAX ||
wstrlcat(buffer, "$", sizeof(buffer)) >= sizeof(buffer) ||
wstrlcat(buffer, buffer2, sizeof(buffer)) >= sizeof(buffer))
goto error;
} else {
if ((i += strlen(tmp)) > PATH_MAX ||
wstrlcat(buffer, tmp, sizeof(buffer)) >= sizeof(buffer))
goto error;
}
}
} else {
buffer[i++] = *path;
path++;
}
}
if (*path!=0)
goto error;
return wstrdup(buffer);
error:
errno = ENAMETOOLONG;
werror(_("could not expand %s"), origpath);
return NULL;
}
/* return address of next char != tok or end of string whichever comes first */
static const char *skipchar(const char *string, char tok)
{
while (*string != 0 && *string == tok)
string++;
return string;
}
/* return address of next char == tok or end of string whichever comes first */
static const char *nextchar(const char *string, char tok)
{
while (*string != 0 && *string != tok)
string++;
return string;
}
/*
*----------------------------------------------------------------------
* findfile--
* Finds a file in a : separated list of paths. ~ expansion is also
* done.
*
* Returns:
* The complete path for the file (in a newly allocated string) or
* NULL if the file was not found.
*
* Side effects:
* A new string is allocated. It must be freed later.
*
*----------------------------------------------------------------------
*/
char *wfindfile(const char *paths, const char *file)
{
char *path;
const char *tmp, *tmp2;
int len, flen;
char *fullpath;
if (!file)
return NULL;
if (*file == '/' || *file == '~' || *file == '$' || !paths || *paths == 0) {
if (access(file, F_OK) < 0) {
fullpath = wexpandpath(file);
if (!fullpath)
return NULL;
if (access(fullpath, F_OK) < 0) {
wfree(fullpath);
return NULL;
} else {
return fullpath;
}
} else {
return wstrdup(file);
}
}
flen = strlen(file);
tmp = paths;
while (*tmp) {
tmp = skipchar(tmp, ':');
if (*tmp == 0)
break;
tmp2 = nextchar(tmp, ':');
len = tmp2 - tmp;
path = wmalloc(len + flen + 2);
path = memcpy(path, tmp, len);
path[len] = 0;
if (path[len - 1] != '/' &&
wstrlcat(path, "/", len + flen + 2) >= len + flen + 2) {
wfree(path);
return NULL;
}
if (wstrlcat(path, file, len + flen + 2) >= len + flen + 2) {
wfree(path);
return NULL;
}
fullpath = wexpandpath(path);
wfree(path);
if (fullpath) {
if (access(fullpath, F_OK) == 0) {
return fullpath;
}
wfree(fullpath);
}
tmp = tmp2;
}
return NULL;
}
char *wfindfileinlist(char *const *path_list, const char *file)
{
int i;
char *path;
int len, flen;
char *fullpath;
if (!file)
return NULL;
if (*file == '/' || *file == '~' || !path_list) {
if (access(file, F_OK) < 0) {
fullpath = wexpandpath(file);
if (!fullpath)
return NULL;
if (access(fullpath, F_OK) < 0) {
wfree(fullpath);
return NULL;
} else {
return fullpath;
}
} else {
return wstrdup(file);
}
}
flen = strlen(file);
for (i = 0; path_list[i] != NULL; i++) {
len = strlen(path_list[i]);
path = wmalloc(len + flen + 2);
path = memcpy(path, path_list[i], len);
path[len] = 0;
if (wstrlcat(path, "/", len + flen + 2) >= len + flen + 2 ||
wstrlcat(path, file, len + flen + 2) >= len + flen + 2) {
wfree(path);
return NULL;
}
/* expand tilde */
fullpath = wexpandpath(path);
wfree(path);
if (fullpath) {
/* check if file exists */
if (access(fullpath, F_OK) == 0) {
return fullpath;
}
wfree(fullpath);
}
}
return NULL;
}
char *wfindfileinarray(WMPropList *array, const char *file)
{
@@ -419,105 +94,3 @@ char *wfindfileinarray(WMPropList *array, const char *file)
}
return NULL;
}
int wcopy_file(const char *dest_dir, const char *src_file, const char *dest_file)
{
char *path_dst;
int fd_src, fd_dst;
struct stat stat_src;
mode_t permission_dst;
const size_t buffer_size = 2 * 1024 * 1024; /* 4MB is a decent start choice to allow the OS to take advantage of modern disk's performance */
char *buffer; /* The buffer is not created on the stack to avoid possible stack overflow as our buffer is big */
try_again_src:
fd_src = open(src_file, O_RDONLY | O_NOFOLLOW);
if (fd_src == -1) {
if (errno == EINTR)
goto try_again_src;
werror(_("Could not open input file \"%s\": %s"), src_file, strerror(errno));
return -1;
}
/* Only accept to copy regular files */
if (fstat(fd_src, &stat_src) != 0 || !S_ISREG(stat_src.st_mode)) {
close(fd_src);
return -1;
}
path_dst = wstrconcat(dest_dir, dest_file);
try_again_dst:
fd_dst = open(path_dst, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd_dst == -1) {
if (errno == EINTR)
goto try_again_dst;
werror(_("Could not create target file \"%s\": %s"), path_dst, strerror(errno));
wfree(path_dst);
close(fd_src);
return -1;
}
buffer = wmalloc(buffer_size);
if (buffer == NULL) {
werror(_("could not allocate memory for the copy buffer"));
close(fd_dst);
goto cleanup_and_return_failure;
}
for (;;) {
ssize_t size_data;
const char *write_ptr;
size_t write_remain;
try_again_read:
size_data = read(fd_src, buffer, buffer_size);
if (size_data == 0)
break; /* End of File have been reached */
if (size_data < 0) {
if (errno == EINTR)
goto try_again_read;
werror(_("could not read from file \"%s\": %s"), src_file, strerror(errno));
close(fd_dst);
goto cleanup_and_return_failure;
}
write_ptr = buffer;
write_remain = size_data;
while (write_remain > 0) {
ssize_t write_done;
try_again_write:
write_done = write(fd_dst, write_ptr, write_remain);
if (write_done < 0) {
if (errno == EINTR)
goto try_again_write;
werror(_("could not write data to file \"%s\": %s"), path_dst, strerror(errno));
close(fd_dst);
goto cleanup_and_return_failure;
}
write_ptr += write_done;
write_remain -= write_done;
}
}
/* Keep only the permission-related part of the field: */
permission_dst = stat_src.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX);
if (fchmod(fd_dst, permission_dst) != 0)
wwarning(_("could not set permission 0%03o on file \"%s\": %s"),
permission_dst, path_dst, strerror(errno));
if (close(fd_dst) != 0) {
werror(_("could not close the file \"%s\": %s"), path_dst, strerror(errno));
cleanup_and_return_failure:
wfree(buffer);
close(fd_src);
unlink(path_dst);
wfree(path_dst);
return -1;
}
wfree(buffer);
wfree(path_dst);
close(fd_src);
return 0;
}
+5 -5
View File
@@ -126,7 +126,7 @@ WMHandlerID WMAddTimerHandler(int milliseconds, WMCallback * callback, void *cda
{
TimerHandler *handler;
handler = wmalloc(sizeof(TimerHandler));
handler = malloc(sizeof(TimerHandler));
if (!handler)
return NULL;
@@ -214,7 +214,7 @@ WMHandlerID WMAddIdleHandler(WMCallback * callback, void *cdata)
{
IdleHandler *handler;
handler = wmalloc(sizeof(IdleHandler));
handler = malloc(sizeof(IdleHandler));
if (!handler)
return NULL;
@@ -279,7 +279,7 @@ Bool W_CheckIdleHandlers(void)
return (idleHandler != NULL && WMGetArrayItemCount(idleHandler) > 0);
}
handlerCopy = WMDuplicateArray(idleHandler);
handlerCopy = WMCreateArrayWithArray(idleHandler);
WM_ITERATE_ARRAY(handlerCopy, handler, iter) {
/* check if the handler still exist or was removed by a callback */
@@ -429,7 +429,7 @@ Bool W_HandleInputEvents(Bool waitForInput, int inputfd)
count = poll(fds, nfds + extrafd, timeout);
if (count > 0 && nfds > 0) {
WMArray *handlerCopy = WMDuplicateArray(inputHandler);
WMArray *handlerCopy = WMCreateArrayWithArray(inputHandler);
int mask;
/* use WM_ITERATE_ARRAY() here */
@@ -527,7 +527,7 @@ Bool W_HandleInputEvents(Bool waitForInput, int inputfd)
count = select(1 + maxfd, &rset, &wset, &eset, timeoutPtr);
if (count > 0 && nfds > 0) {
WMArray *handlerCopy = WMDuplicateArray(inputHandler);
WMArray *handlerCopy = WMCreateArrayWithArray(inputHandler);
int mask;
/* use WM_ITERATE_ARRAY() here */
-55
View File
@@ -1,55 +0,0 @@
/*
* 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;
}
+3 -1
View File
@@ -536,12 +536,14 @@ found_end_define_fname:
while (*src != '\0') {
idx = 0;
if (*src == '~') {
const char *home = wgethomedir();
char *home_head = wgethomedir();
char *home = home_head;;
while (*home != '\0') {
if (idx < sizeof(buffer) - 2)
buffer[idx++] = *home;
home++;
}
wfree(home_head);
src++;
}
-14
View File
@@ -942,20 +942,6 @@ WMPropList *WMCreatePLDataWithBytes(const unsigned char *bytes, unsigned int len
return plist;
}
WMPropList *WMCreatePLDataWithBytesNoCopy(unsigned char *bytes, unsigned int length, WMFreeDataProc * destructor)
{
WMPropList *plist;
wassertrv(bytes != NULL, NULL);
plist = (WMPropList *) wmalloc(sizeof(W_PropList));
plist->type = WPLData;
plist->d.data = WMCreateDataWithBytesNoCopy(bytes, length, destructor);
plist->retainCount = 1;
return plist;
}
WMPropList *WMCreatePLArray(WMPropList * elem, ...)
{
WMPropList *plist, *nelem;
+4 -3
View File
@@ -237,7 +237,7 @@ static void handleRequestEvent(XEvent * event)
}
/* delete handlers */
copy = WMDuplicateArray(selHandlers);
copy = WMCreateArrayWithArray(selHandlers);
WM_ITERATE_ARRAY(copy, handler, iter) {
if (handler && handler->flags.delete_pending) {
WMDeleteSelectionHandler(handler->view, handler->selection, handler->timestamp);
@@ -261,8 +261,9 @@ static WMData *getSelectionData(Display * dpy, Window win, Atom where)
bpi = bits / 8;
wdata = WMCreateDataWithBytesNoCopy(data, len * bpi, (void *) XFree);
wdata = WMCreateDataWithBytes(data, len * bpi);
WMSetDataFormat(wdata, bits);
XFree(data);
return wdata;
}
@@ -300,7 +301,7 @@ static void handleNotifyEvent(XEvent * event)
}
/* delete callbacks */
copy = WMDuplicateArray(selCallbacks);
copy = WMCreateArrayWithArray(selCallbacks);
WM_ITERATE_ARRAY(copy, handler, iter) {
if (handler && handler->flags.delete_pending) {
WMDeleteSelectionCallback(handler->view, handler->selection, handler->timestamp);
+2 -3
View File
@@ -51,7 +51,7 @@ const char *wusergnusteppath(void)
static const char subdir[] = "/" GSUSER_SUBDIR;
static char *path = NULL;
char *gspath;
const char *h;
char *h;
int pathlen;
if (path)
@@ -69,13 +69,12 @@ const char *wusergnusteppath(void)
}
h = wgethomedir();
if (!h)
return NULL;
pathlen = strlen(h);
path = wmalloc(pathlen + sizeof(subdir));
strcpy(path, h);
strcpy(path + pathlen, subdir);
wfree(h);
return path;
}
+6 -3
View File
@@ -2994,10 +2994,13 @@ static void customPaletteMenuNewFromFile(W_ColorPanel * panel)
int i;
RImage *tmpImg = NULL;
if ((!panel->lastBrowseDir) || (strcmp(panel->lastBrowseDir, "\0") == 0))
spath = wexpandpath(wgethomedir());
else
if ((!panel->lastBrowseDir) || (strcmp(panel->lastBrowseDir, "\0") == 0)) {
char *homedir = wgethomedir();
spath = wexpandpath(homedir);
wfree(homedir);
} else {
spath = wexpandpath(panel->lastBrowseDir);
}
browseP = WMGetOpenPanel(scr);
WMSetFilePanelCanChooseDirectories(browseP, 0);
+2 -4
View File
@@ -761,17 +761,15 @@ static void goFloppy(WMWidget *widget, void *p_panel)
static void goHome(WMWidget *widget, void *p_panel)
{
WMFilePanel *panel = p_panel;
const char *home;
char *home;
/* Parameter not used, but tell the compiler that it is ok */
(void) widget;
/* home is statically allocated. Don't free it! */
home = wgethomedir();
if (!home)
return;
WMSetFilePanelDirectory(panel, home);
wfree(home);
}
static void handleEvents(XEvent * event, void *data)
+9 -3
View File
@@ -51,12 +51,15 @@ static char *xlfdToFcName(const char *xlfd)
{
FcPattern *pattern;
char *fname;
char *result;
pattern = xlfdToFcPattern(xlfd);
fname = (char *)FcNameUnparse(pattern);
result = wstrdup(fname);
free(fname);
FcPatternDestroy(pattern);
return fname;
return result;
}
static Bool hasProperty(FcPattern * pattern, const char *property)
@@ -92,6 +95,7 @@ static Bool hasPropertyWithStringValue(FcPattern * pattern, const char *object,
static char *makeFontOfSize(const char *font, int size, const char *fallback)
{
FcPattern *pattern;
char *name;
char *result;
if (font[0] == '-') {
@@ -115,7 +119,9 @@ static char *makeFontOfSize(const char *font, int size, const char *fallback)
/*FcPatternPrint(pattern); */
result = (char *)FcNameUnparse(pattern);
name = (char *)FcNameUnparse(pattern);
result = wstrdup(name);
free(name);
FcPatternDestroy(pattern);
return result;
@@ -421,7 +427,7 @@ WMFont *WMCopyFontWithStyle(WMScreen * scrPtr, WMFont * font, WMFontStyle style)
name = (char *)FcNameUnparse(pattern);
copy = WMCreateFont(scrPtr, name);
FcPatternDestroy(pattern);
wfree(name);
free(name);
return copy;
}
+2 -1
View File
@@ -610,9 +610,10 @@ WMScreen *WMCreateScreenWithRContext(Display * display, int screen, RContext * c
assert(W_ApplicationInitialized());
}
scrPtr = wmalloc(sizeof(W_Screen));
scrPtr = malloc(sizeof(W_Screen));
if (!scrPtr)
return NULL;
memset(scrPtr, 0, sizeof(W_Screen));
scrPtr->aflags.hasAppIcon = 1;
+6 -2
View File
@@ -1110,7 +1110,7 @@ static void deleteTexture(WMWidget * w, void *data)
static void extractTexture(WMWidget * w, void *data)
{
_Panel *panel = (_Panel *) data;
char *path;
char *path, *homedir;
WMOpenPanel *opanel;
WMScreen *scr = WMWidgetScreen(w);
@@ -1118,13 +1118,17 @@ static void extractTexture(WMWidget * w, void *data)
WMSetFilePanelCanChooseDirectories(opanel, False);
WMSetFilePanelCanChooseFiles(opanel, True);
if (WMRunModalFilePanelForDirectory(opanel, panel->parent, wgethomedir(), _("Select File"), NULL)) {
homedir = wgethomedir();
if (WMRunModalFilePanelForDirectory(opanel, panel->parent, homedir, _("Select File"), NULL)) {
path = WMGetFilePanelFileName(opanel);
OpenExtractPanelFor(panel);
wfree(path);
}
if (homedir) {
wfree(homedir);
}
}
static void changePage(WMWidget * w, void *data)
+1 -1
View File
@@ -129,7 +129,7 @@ static void autoDelayChanged(void *observerData, WMNotification *notification)
}
char *value = WMGetTextFieldText(anAutoDelayT);
adjustButtonSelectionBasedOnValue(panel, row, value);
wfree(value);
free(value);
return;
}
}
+1 -1
View File
@@ -138,7 +138,7 @@ static void storeData(_Panel * panel)
if (sscanf(str, "%i", &i) != 1)
i = 0;
SetIntegerForKey(i, "RaiseDelay");
wfree(str);
free(str);
SetBoolForKey(WMGetButtonSelected(panel->ignB), "IgnoreFocusClick");
SetBoolForKey(WMGetButtonSelected(panel->newB), "AutoFocus");
+5 -1
View File
@@ -288,6 +288,7 @@ static char *getSelectedFont(_Panel * panel, FcChar8 * curfont)
WMListItem *item;
FcPattern *pat;
char *name;
char *result;
if (curfont)
pat = FcNameParse(curfont);
@@ -321,9 +322,12 @@ static char *getSelectedFont(_Panel * panel, FcChar8 * curfont)
}
name = (char *)FcNameUnparse(pat);
result = wstrdup(name);
free(name);
FcPatternDestroy(pat);
return name;
return result;
}
static void updateSampleFont(_Panel * panel)
+1 -1
View File
@@ -147,7 +147,7 @@ static void storeData(_Panel * panel)
if (sscanf(str, "%i", &i) != 1)
i = 0;
SetIntegerForKey(i, "HotCornerDelay");
wfree(str);
free(str);
SetIntegerForKey(WMGetSliderValue(panel->hceS), "HotCornerEdge");
+1 -1
View File
@@ -66,9 +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/libWINGs.la\
$(top_builddir)/WINGs/libWUtil.la\
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
$(top_builddir)/wrlib/libwraster.la \
@XLFLAGS@ @XLIBS@ \
@LIBM@ \
+1 -1
View File
@@ -627,7 +627,7 @@ static void browseImageCallback(WMWidget *w, void *data)
WMSetFilePanelCanChooseFiles(opanel, True);
if (!ipath)
ipath = wstrdup(wgethomedir());
ipath = wgethomedir();
if (WMRunModalFilePanelForDirectory(opanel, panel->win, ipath, _("Open Image"), NULL)) {
char *path, *fullpath;
-2
View File
@@ -87,8 +87,6 @@ int main(int argc, char **argv)
int i;
char *display_name = "";
wsetabort(wAbort);
memset(DeadHandlers, 0, sizeof(DeadHandlers));
WMInitializeApplication("WPrefs", &argc, argv);
+5 -18
View File
@@ -54,10 +54,12 @@ AC_CHECK_PROG(CARGO, [cargo], [yes], [no])
AS_IF(test x$CARGO = xno,
AC_MSG_ERROR([cargo is required. Please set the CARGO environment variable or install the Rust toolchain from https://www.rust-lang.org/])
)
AC_SUBST(CARGO, [cargo])
AC_CHECK_PROG(RUSTC, [rustc], [yes], [no])
AS_IF(test x$RUSTC = xno,
AC_MSG_ERROR([rustc is required. Please set the RUSTC environment variable or install the Rust toolchain from https://www.rust-lang.org/])
)
AC_SUBST(RUSTC, [rustc])
dnl libtool library versioning
dnl ==========================
@@ -351,24 +353,6 @@ 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
@@ -973,6 +957,9 @@ 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
View File
@@ -253,14 +253,6 @@ 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
@@ -468,8 +460,6 @@ 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}.
+2 -2
View File
@@ -139,7 +139,7 @@ static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int coun
}
wstrlcpy(title, &slist[*index][pos], sizeof(title));
}
data = wmalloc(sizeof(WAppMenuData));
data = malloc(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);
wfree(data);
free(data);
return NULL;
}
if (rtext[0] != 0)
+32 -3
View File
@@ -39,6 +39,10 @@
#include <time.h>
#include <sys/utsname.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <signal.h>
#ifdef __FreeBSD__
#include <sys/signal.h>
@@ -363,7 +367,7 @@ static void handleHistoryKeyPress(XEvent * event, void *clientData)
case XK_Up:
if (p->histpos < WMGetArrayItemCount(p->history) - 1) {
if (p->histpos == 0)
wfree(WMReplaceInArray(p->history, 0, WMGetTextFieldText(p->panel->text)));
wfree(WMSetInArray(p->history, 0, WMGetTextFieldText(p->panel->text)));
p->histpos++;
WMSetTextFieldText(p->panel->text, WMGetFromArray(p->history, p->histpos));
}
@@ -464,7 +468,7 @@ int wAdvancedInputDialog(WScreen *scr, const char *title, const char *message, c
if (p->panel->result == WAPRDefault) {
result = WMGetTextFieldText(p->panel->text);
wfree(WMReplaceInArray(p->history, 0, wstrdup(result)));
wfree(WMSetInArray(p->history, 0, wstrdup(result)));
SaveHistory(p->history, filename);
} else
result = NULL;
@@ -1357,7 +1361,7 @@ void wShowInfoPanel(WScreen *scr)
char *posn = getPrettyOSName();
if (posn) {
snprintf(buffer, sizeof(buffer), _("Running on: %s (%s)\n"), posn, uts.machine);
wfree(posn);
free(posn);
}
else
snprintf(buffer, sizeof(buffer), _("Running on: %s (%s)\n"), uts.sysname, uts.machine);
@@ -1389,6 +1393,31 @@ 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;
+7 -5
View File
@@ -3194,7 +3194,7 @@ static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
setsid();
#endif
args = wmalloc(sizeof(char *) * (argc + 1));
args = malloc(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)) {
wfree(wm_class);
wfree(wm_instance);
free(wm_class);
free(wm_instance);
return;
}
@@ -3419,8 +3419,10 @@ void wDockTrackWindowLaunch(WDock *dock, Window window)
if (command)
wfree(command);
wfree(wm_class);
wfree(wm_instance);
if (wm_class)
free(wm_class);
if (wm_instance)
free(wm_instance);
}
void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
+5 -5
View File
@@ -141,7 +141,7 @@ WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
{
DeathHandler *handler;
handler = wmalloc(sizeof(DeathHandler));
handler = malloc(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, wfree);
deathHandlers = WMCreateArrayWithDestructor(8, free);
WMAddToArray(deathHandlers, handler);
@@ -164,7 +164,7 @@ static void wdelete_death_handler(WMagicNumber id)
if (!handler || !deathHandlers)
return;
/* array destructor will call wfree(handler) */
/* array destructor will call free(handler) */
WMRemoveFromArray(deathHandlers, handler);
}
@@ -1796,7 +1796,7 @@ static void handleKeyPress(XEvent * event)
}
if (wwin->flags.selected && scr->selected_windows) {
scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
scr->shortcutWindows[widx] = WMCreateArrayWithArray(scr->selected_windows);
/*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
} else {
@@ -1816,7 +1816,7 @@ static void handleKeyPress(XEvent * event)
if (scr->shortcutWindows[widx]) {
WMFreeArray(scr->shortcutWindows[widx]);
}
scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
scr->shortcutWindows[widx] = WMCreateArrayWithArray(scr->selected_windows);
}
}
+1 -1
View File
@@ -40,7 +40,7 @@ WGeometryView *WCreateGeometryView(WMScreen * scr)
widgetClass = W_RegisterUserWidget();
}
gview = wmalloc(sizeof(WGeometryView));
gview = malloc(sizeof(WGeometryView));
if (!gview) {
return NULL;
}
+4 -5
View File
@@ -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 *)wmalloc(wVisualID_len * sizeof(int));
wVisualID = (int *)malloc(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 = wstrdup(user_str);
char *mystr = strdup(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++;
}
wfree(mystr);
free(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 **) wmalloc(argc + 1);
char **a = (char **) malloc(argc + 1);
if (! a) {
werror("out of memory trying to relaunch the application");
Exit(-1);
@@ -624,7 +624,6 @@ 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);
+6 -6
View File
@@ -520,7 +520,7 @@ char *ExpandOptions(WScreen *scr, const char *cmdline)
len = strlen(cmdline);
olen = len + 1;
out = wmalloc(olen);
out = malloc(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 = wrealloc(out, olen);
nout = realloc(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 = wrealloc(out, olen);
nout = realloc(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 = wrealloc(out, olen);
nout = realloc(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 = wrealloc(out, olen);
nout = realloc(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 = wrealloc(out, olen);
nout = realloc(out, olen);
if (!nout) {
wwarning(_("out of memory during expansion of '%s' for command \"%s\""), "%s", cmdline);
goto error;
+6 -6
View File
@@ -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 = wstrdup("default");
*wm_instance = wstrdup("default");
*wm_class = strdup("default");
*wm_instance = strdup("default");
XFree(class_hint);
return False;
}
*wm_instance = wstrdup(class_hint->res_name);
*wm_class = wstrdup(class_hint->res_class);
*wm_instance = strdup(class_hint->res_name);
*wm_class = strdup(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 = wmalloc(sizeof(GNUstepWMAttributes));
*attr = malloc(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 = wmalloc(image->width * image->height * 4 + 4);
tmp = malloc(image->width * image->height * 4 + 4);
if (!tmp) {
wwarning("could not allocate memory to set _WINDOWMAKER_ICON_TILE hint");
return;
+4 -4
View File
@@ -1243,7 +1243,7 @@ static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, co
if (dentry->d_name[0] == '.')
continue;
buffer = wmalloc(strlen(path[i]) + strlen(dentry->d_name) + 4);
buffer = malloc(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
}
}
}
wfree(buffer);
free(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 = wmalloc(length);
buffer = malloc(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 = wmalloc(length);
buffer = malloc(length);
if (!buffer) {
werror(_("out of memory while constructing directory menu %s"), path[data->index]);
break;
+3 -3
View File
@@ -289,9 +289,9 @@ static WMPropList *makeWindowState(WWindow * wwin, WApplication * wapp)
}
if (instance)
wfree(instance);
free(instance);
if (class)
wfree(class);
free(class);
if (command)
wfree(command);
@@ -382,7 +382,7 @@ static pid_t execCommand(WScreen *scr, char *command)
SetupEnvironment(scr);
args = wmalloc(sizeof(char *) * (argc + 1));
args = malloc(sizeof(char *) * (argc + 1));
if (!args)
exit(111);
for (i = 0; i < argc; i++) {
+2 -2
View File
@@ -244,7 +244,7 @@ reinit:
wApplicationSetBouncing(data->wapp, 0);
WMDeleteTimerHandler(data->timer);
wApplicationDestroy(data->wapp);
wfree(data);
free(data);
}
static int bounceDirection(WAppIcon *aicon)
@@ -324,7 +324,7 @@ void wAppBounce(WApplication *wapp)
wApplicationIncrementRefcount(wapp);
wApplicationSetBouncing(wapp, 1);
AppBouncerData *data = (AppBouncerData *)wmalloc(sizeof(AppBouncerData));
AppBouncerData *data = (AppBouncerData *)malloc(sizeof(AppBouncerData));
data->wapp = wapp;
data->count = data->pow = 0;
data->dir = bounceDirection(wApplicationGetAppIcon(wapp));
+3 -2
View File
@@ -132,7 +132,7 @@ static void changeImage(WSwitchPanel *panel, int idecks, int selected, Bool dim,
if (flags == desired && !force)
return;
WMReplaceInArray(panel->flags, idecks, (void *) (uintptr_t) desired);
WMSetInArray(panel->flags, idecks, (void *) (uintptr_t) desired);
if (!panel->bg && !panel->tile && !selected)
WMSetFrameRelief(icon, WRFlat);
@@ -357,7 +357,8 @@ static void drawTitle(WSwitchPanel *panel, int idecks, const char *title)
WMSetLabelText(panel->label, ntitle);
}
wfree(ntitle);
if (ntitle)
free(ntitle);
}
static WMArray *makeWindowListArray(WScreen *scr, int include_unmapped, Bool class_only)
+6 -6
View File
@@ -950,10 +950,10 @@ WWindow *wManageWindow(WScreen *scr, Window window)
}
if (instance)
wfree(instance);
free(instance);
if (class)
wfree(class);
free(class);
#undef ADEQUATE
}
@@ -2602,7 +2602,7 @@ void wWindowSetShape(WWindow * wwin)
if (!rects)
goto alt_code;
urec = wmalloc(sizeof(XRectangle) * (count + 2));
urec = malloc(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 = wmalloc(sizeof(WWindowState));
wstate = malloc(sizeof(WWindowState));
if (!wstate)
return NULL;
@@ -2841,9 +2841,9 @@ WMagicNumber wWindowGetSavedState(Window win)
if (command)
wfree(command);
if (instance)
wfree(instance);
free(instance);
if (class)
wfree(class);
free(class);
return wstate;
}
+1 -1
View File
@@ -346,7 +346,7 @@ static void makeShortcutCommand(WMenu * menu, WMenuEntry * entry)
}
if (wwin->flags.selected && scr->selected_windows) {
scr->shortcutWindows[index] = WMDuplicateArray(scr->selected_windows);
scr->shortcutWindows[index] = WMCreateArrayWithArray(scr->selected_windows);
/*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
} else {
+2 -2
View File
@@ -256,7 +256,7 @@ static void wXDNDGetTypeList(Display *dpy, Window window)
return;
}
typelist = wmalloc((count + 1) * sizeof(Atom));
typelist = malloc((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);
wfree(typelist);
free(typelist);
}
Bool wXDNDProcessClientMessage(XClientMessageEvent *event)
+24 -20
View File
@@ -18,70 +18,74 @@ AM_CPPFLAGS = \
liblist= @LIBRARY_SEARCH_PATH@ @INTLIBS@
wdwrite_LDADD = $(top_builddir)/WINGs/libWUtil.la \
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
wdwrite_LDADD = \
$(top_builddir)/WINGs/libWUtil.la \
$(top_builddir)/wutil-rs/target/debug/libwutil_rs.a \
$(liblist)
wdread_LDADD = $(top_builddir)/WINGs/libWUtil.la \
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
wdread_LDADD = \
$(top_builddir)/WINGs/libWUtil.la \
$(top_builddir)/wutil-rs/target/debug/libwutil_rs.a \
$(liblist)
wxcopy_LDADD = @XLFLAGS@ @XLIBS@ \
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a
wxcopy_LDADD = @XLFLAGS@ @XLIBS@
wxpaste_LDADD = @XLFLAGS@ @XLIBS@
getstyle_LDADD = $(top_builddir)/WINGs/libWUtil.la\
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
getstyle_LDADD = \
$(top_builddir)/WINGs/libWUtil.la \
$(top_builddir)/wutil-rs/target/debug/libwutil_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\
$(top_builddir)/wutil-rs/target/debug/libwutil_rs.a \
@XLFLAGS@ @XLIBS@ $(liblist)
setstyle_SOURCES = setstyle.c fontconv.c common.h
convertfonts_LDADD = $(top_builddir)/WINGs/libWUtil.la \
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
convertfonts_LDADD = \
$(top_builddir)/WINGs/libWUtil.la \
$(top_builddir)/wutil-rs/target/debug/libwutil_rs.a \
$(liblist)
convertfonts_SOURCES = convertfonts.c fontconv.c common.h
seticons_LDADD= $(top_builddir)/WINGs/libWUtil.la\
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
seticons_LDADD= \
$(top_builddir)/WINGs/libWUtil.la \
$(top_builddir)/wutil-rs/target/debug/libwutil_rs.a \
$(liblist)
geticonset_LDADD= $(top_builddir)/WINGs/libWUtil.la\
$(top_builddir)/wmaker-rs/target/debug/libwmaker_rs.a\
geticonset_LDADD= \
$(top_builddir)/WINGs/libWUtil.la \
$(top_builddir)/wutil-rs/target/debug/libwutil_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)/wutil-rs/target/debug/libwutil_rs.a \
$(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\
$(top_builddir)/wutil-rs/target/debug/libwutil_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\
$(top_builddir)/wutil-rs/target/debug/libwutil_rs.a \
@INTLIBS@
wmmenugen_SOURCES = wmmenugen.c wmmenugen.h wmmenugen_misc.c \
@@ -93,7 +97,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\
$(top_builddir)/wutil-rs/target/debug/libwutil_rs.a \
@XLFLAGS@ @XLIBS@ @GFXLIBS@ \
@PANGO_LIBS@ @PTHREAD_LIBS@ @LIBEXIF@
+3 -3
View File
@@ -242,7 +242,7 @@ static void makeThemePack(WMPropList * style, const char *themeName)
WMDeleteFromPLArray(value, 1);
WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
wfree(newPath);
free(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));
wfree(newPath);
free(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));
wfree(newPath);
free(newPath);
} else {
findCopyFile(themeDir, WMGetFromPLString(file));
}
+1 -1
View File
@@ -465,7 +465,7 @@ int main(int argc, char **argv)
}
buf[strlen(buf) - 6 /* strlen("/style") */] = '\0';
homedir = wstrdup(wgethomedir());
homedir = wgethomedir();
if (strlen(homedir) > 1 && /* this is insane, wgethomedir() returns `/' on error */
strncmp(homedir, buf, strlen(homedir)) == 0) {
/* theme pack is under ${HOME}; exchange ${HOME} part
+9 -9
View File
@@ -202,7 +202,7 @@ int change_title(XTextProperty *prop, char *filename)
XSetWMName(dpy, win, prop);
if (prop->value)
XFree(prop->value);
wfree(combined_title);
free(combined_title);
return EXIT_SUCCESS;
}
@@ -596,8 +596,8 @@ int linked_list_add(linked_list_t *list, const void *data)
{
link_t *link;
/* wmalloc zeros the buffer it returns, so the "next" is zero. */
link = wmalloc(sizeof(link_t));
/* calloc sets the "next" field to zero. */
link = calloc(1, 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)
wfree((char *)link->data);
wfree(link);
free((char *)link->data);
free(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, wstrdup(dirpath));
linked_list_add(li, strdup(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);
wfree(dir[idx]);
free(dir[idx]);
if ((lstat(path, &stDirInfo) == 0) && !S_ISDIR(stDirInfo.st_mode))
linked_list_add(li, wstrdup(path));
linked_list_add(li, strdup(path));
}
wfree(dir);
free(dir);
return li->first;
}
+1 -1
View File
@@ -121,7 +121,7 @@ void parse_locale(const char *what, char **language, char **country, char **enco
*language = wstrdup(e);
out:
wfree(e);
free(e);
return;
}
+2 -2
View File
@@ -398,7 +398,7 @@ static BackgroundTexture *parseTexture(RContext * rc, char *text)
RGradientStyle gtype;
int iwidth, iheight;
colors = wmalloc(sizeof(RColor *) * (count - 1));
colors = malloc(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] = wmalloc(sizeof(RColor)))) {
if (!(colors[i - 2] = malloc(sizeof(RColor)))) {
wwarning("out of memory while parsing texture");
for (j = 0; colors[j] != NULL; j++)
+7 -4
View File
@@ -26,8 +26,6 @@
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <WINGs/WUtil.h>
#include "../src/wconfig.h"
#define LINESIZE (4*1024)
@@ -199,7 +197,7 @@ int main(int argc, char **argv)
break;
}
if (buf_len == 0) {
nbuf = wmalloc(buf_len = l + nl + 1);
nbuf = malloc(buf_len = l + nl + 1);
} else if (buf_len < l + nl + 1) {
/*
* To avoid terrible performance on big input buffers,
@@ -207,7 +205,12 @@ int main(int argc, char **argv)
* current line.
*/
buf_len = 2 * buf_len + nl + 1;
nbuf = wrealloc(buf, buf_len);
/* some realloc implementations don't do malloc if buf==NULL */
if (buf == NULL) {
nbuf = malloc(buf_len);
} else {
nbuf = realloc(buf, buf_len);
}
} else {
nbuf = buf;
}
+1 -2
View File
@@ -1,11 +1,10 @@
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
View File
@@ -1,180 +0,0 @@
//! 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);
}
}
+1 -2
View File
@@ -6,8 +6,7 @@ lib_LTLIBRARIES = libWMaker.la
include_HEADERS = WMaker.h
AM_CPPFLAGS = $(DFLAGS) @XCFLAGS@ \
-I$(top_srcdir)/WINGs -I$(top_builddir)/WINGs
AM_CPPFLAGS = $(DFLAGS) @XCFLAGS@
libWMaker_la_LIBADD = @XLFLAGS@ @XLIBS@
+5 -7
View File
@@ -24,8 +24,6 @@
#include <stdlib.h>
#include <string.h>
#include <WINGs/WUtil.h>
#include "WMaker.h"
#include "app.h"
@@ -33,7 +31,7 @@ WMAppContext *WMAppCreateWithMain(Display * display, int screen_number, Window m
{
wmAppContext *ctx;
ctx = wmalloc(sizeof(wmAppContext));
ctx = malloc(sizeof(wmAppContext));
if (!ctx)
return NULL;
@@ -41,9 +39,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 = wmalloc(sizeof(Window));
ctx->windows = malloc(sizeof(Window));
if (!ctx->windows) {
wfree(ctx);
free(ctx);
return NULL;
}
ctx->win_count = 1;
@@ -60,13 +58,13 @@ int WMAppAddWindow(WMAppContext * app, Window window)
{
Window *win;
win = wmalloc(sizeof(Window) * (app->win_count + 1));
win = malloc(sizeof(Window) * (app->win_count + 1));
if (!win)
return False;
memcpy(win, app->windows, sizeof(Window) * app->win_count);
wfree(app->windows);
free(app->windows);
win[app->win_count] = window;
app->windows = win;
+14 -16
View File
@@ -26,8 +26,6 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <WINGs/WUtil.h>
#include "WMaker.h"
#include "app.h"
#include "menu.h"
@@ -39,7 +37,7 @@ WMMenu *WMMenuCreate(WMAppContext * app, char *title)
if (strlen(title) > 255)
return NULL;
menu = wmalloc(sizeof(wmMenu));
menu = malloc(sizeof(wmMenu));
if (!menu)
return NULL;
@@ -52,12 +50,12 @@ WMMenu *WMMenuCreate(WMAppContext * app, char *title)
menu->realized = False;
menu->code = app->last_menu_tag++;
menu->entryline = wmalloc(strlen(title) + 32);
menu->entryline2 = wmalloc(32);
menu->entryline = malloc(strlen(title) + 32);
menu->entryline2 = malloc(32);
if (!menu->entryline || !menu->entryline2) {
if (menu->entryline)
wfree(menu->entryline);
wfree(menu);
free(menu->entryline);
free(menu);
return NULL;
}
sprintf(menu->entryline, "%i %i %s", wmBeginMenu, menu->code, title);
@@ -79,13 +77,13 @@ WMMenuAddItem(WMMenu * menu, char *text, WMMenuAction action,
if (strlen(text) > 255)
return -1;
entry = wmalloc(sizeof(wmMenuEntry));
entry = malloc(sizeof(wmMenuEntry));
if (!entry)
return -1;
entry->entryline = wmalloc(strlen(text) + 100);
entry->entryline = malloc(strlen(text) + 100);
if (!entry->entryline) {
wfree(entry);
free(entry);
return -1;
}
@@ -127,13 +125,13 @@ int WMMenuAddSubmenu(WMMenu * menu, char *text, WMMenu * submenu)
if (strlen(text) > 255)
return -1;
entry = wmalloc(sizeof(wmMenuEntry));
entry = malloc(sizeof(wmMenuEntry));
if (!entry)
return -1;
entry->entryline = wmalloc(strlen(text) + 100);
entry->entryline = malloc(strlen(text) + 100);
if (!entry->entryline) {
wfree(entry);
free(entry);
return -1;
}
@@ -219,7 +217,7 @@ int WMRealizeMenus(WMAppContext * app)
return True;
count++;
slist = wmalloc(count * sizeof(char *));
slist = malloc(count * sizeof(char *));
if (!slist) {
return False;
}
@@ -229,10 +227,10 @@ int WMRealizeMenus(WMAppContext * app)
addItems(slist, &i, app->main_menu);
if (!XStringListToTextProperty(slist, i, &text_prop)) {
wfree(slist);
free(slist);
return False;
}
wfree(slist);
free(slist);
XSetTextProperty(app->dpy, app->main_window, &text_prop, getatom(app->dpy));
XFree(text_prop.value);
+1 -2
View File
@@ -82,8 +82,7 @@ libwraster_la_SOURCES += load_magick.c
endif
AM_CFLAGS = @MAGICKFLAGS@
AM_CPPFLAGS = $(DFLAGS) @HEADER_SEARCH_PATH@ \
-I$(top_srcdir)/WINGs -I$(top_builddir)/WINGs
AM_CPPFLAGS = $(DFLAGS) @HEADER_SEARCH_PATH@
libwraster_la_LIBADD = @LIBRARY_SEARCH_PATH@ @GFXLIBS@ @MAGICKLIBS@ @XLIBS@ @LIBXMU@ -lm
+15 -17
View File
@@ -37,8 +37,6 @@
#include <math.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "scale.h"
#include "wr_i18n.h"
@@ -106,17 +104,17 @@ static Bool allocateStandardPseudoColor(RContext * ctx, XStandardColormap * stdc
return False;
}
ctx->colors = wmalloc(sizeof(XColor) * ctx->ncolors);
ctx->colors = malloc(sizeof(XColor) * ctx->ncolors);
if (!ctx->colors) {
RErrorCode = RERR_NOMEMORY;
return False;
}
ctx->pixels = wmalloc(sizeof(unsigned long) * ctx->ncolors);
ctx->pixels = malloc(sizeof(unsigned long) * ctx->ncolors);
if (!ctx->pixels) {
wfree(ctx->colors);
free(ctx->colors);
ctx->colors = NULL;
RErrorCode = RERR_NOMEMORY;
@@ -248,15 +246,15 @@ static Bool allocatePseudoColor(RContext *ctx)
assert(cpc >= 2 && ncolors <= (1 << ctx->depth));
colors = wmalloc(sizeof(XColor) * ncolors);
colors = malloc(sizeof(XColor) * ncolors);
if (!colors) {
RErrorCode = RERR_NOMEMORY;
return False;
}
ctx->pixels = wmalloc(sizeof(unsigned long) * ncolors);
ctx->pixels = malloc(sizeof(unsigned long) * ncolors);
if (!ctx->pixels) {
wfree(colors);
free(colors);
RErrorCode = RERR_NOMEMORY;
return False;
}
@@ -345,7 +343,7 @@ static XColor *allocateGrayScale(RContext * ctx)
ctx->attribs->render_mode = RBestMatchRendering;
}
colors = wmalloc(sizeof(XColor) * ncolors);
colors = malloc(sizeof(XColor) * ncolors);
if (!colors) {
RErrorCode = RERR_NOMEMORY;
return False;
@@ -528,7 +526,7 @@ RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttribu
RContext *context;
XGCValues gcv;
context = wmalloc(sizeof(RContext));
context = malloc(sizeof(RContext));
if (!context) {
RErrorCode = RERR_NOMEMORY;
return NULL;
@@ -539,9 +537,9 @@ RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttribu
context->screen_number = screen_number;
context->attribs = wmalloc(sizeof(RContextAttributes));
context->attribs = malloc(sizeof(RContextAttributes));
if (!context->attribs) {
wfree(context);
free(context);
RErrorCode = RERR_NOMEMORY;
return NULL;
}
@@ -570,7 +568,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) {
wfree(context);
free(context);
RErrorCode = RERR_BADVISUALID;
return NULL;
}
@@ -617,13 +615,13 @@ RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttribu
if (context->vclass == PseudoColor || context->vclass == StaticColor) {
if (!setupPseudoColorColormap(context)) {
wfree(context);
free(context);
return NULL;
}
} else if (context->vclass == GrayScale || context->vclass == StaticGray) {
context->colors = allocateGrayScale(context);
if (!context->colors) {
wfree(context);
free(context);
return NULL;
}
} else if (context->vclass == TrueColor) {
@@ -670,9 +668,9 @@ void RDestroyContext(RContext *context)
if ((context->attribs->flags & RC_VisualID) &&
!(context->attribs->flags & RC_DefaultVisual))
XDestroyWindow(context->dpy, context->drawable);
wfree(context->attribs);
free(context->attribs);
}
wfree(context);
free(context);
}
}
+21 -23
View File
@@ -33,15 +33,13 @@
#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) wfree(n)
#define NFREE(n) if (n) free(n)
#define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
@@ -72,7 +70,7 @@ static void release_conversion_table(void)
RConversionTable *tmp_to_delete = tmp;
tmp = tmp->next;
wfree(tmp_to_delete);
free(tmp_to_delete);
}
conversionTable = NULL;
}
@@ -85,7 +83,7 @@ static void release_std_conversion_table(void)
RStdConversionTable *tmp_to_delete = tmp;
tmp = tmp->next;
wfree(tmp_to_delete);
free(tmp_to_delete);
}
stdConversionTable = NULL;
}
@@ -110,7 +108,7 @@ static unsigned short *computeTable(unsigned short mask)
if (tmp)
return tmp->table;
tmp = (RConversionTable *) wmalloc(sizeof(RConversionTable));
tmp = (RConversionTable *) malloc(sizeof(RConversionTable));
if (tmp == NULL)
return NULL;
@@ -137,7 +135,7 @@ static unsigned int *computeStdTable(unsigned int mult, unsigned int max)
if (tmp)
return tmp->table;
tmp = (RStdConversionTable *) wmalloc(sizeof(RStdConversionTable));
tmp = (RStdConversionTable *) malloc(sizeof(RStdConversionTable));
if (tmp == NULL)
return NULL;
@@ -374,8 +372,8 @@ static RXImage *image2TrueColor(RContext * ctx, RImage * image)
signed char *nerr;
int ch = (HAS_ALPHA(image) ? 4 : 3);
err = wmalloc(ch * (image->width + 2));
nerr = wmalloc(ch * (image->width + 2));
err = malloc(ch * (image->width + 2));
nerr = malloc(ch * (image->width + 2));
if (!err || !nerr) {
NFREE(err);
NFREE(nerr);
@@ -389,8 +387,8 @@ static RXImage *image2TrueColor(RContext * ctx, RImage * image)
convertTrueColor_generic(ximg, image, err, nerr,
rtable, gtable, btable, dr, dg, db, roffs, goffs, boffs);
wfree(err);
wfree(nerr);
free(err);
free(nerr);
}
}
@@ -542,8 +540,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 = wmalloc(4 * (image->width + 3));
nerr = wmalloc(4 * (image->width + 3));
err = malloc(4 * (image->width + 3));
nerr = malloc(4 * (image->width + 3));
if (!err || !nerr) {
NFREE(err);
NFREE(nerr);
@@ -557,8 +555,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);
wfree(err);
wfree(nerr);
free(err);
free(nerr);
}
return ximg;
@@ -620,8 +618,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 *)wmalloc(3 * (image->width + 2) * sizeof(short));
nerr = (short *)wmalloc(3 * (image->width + 2) * sizeof(short));
err = (short *)malloc(3 * (image->width + 2) * sizeof(short));
nerr = (short *)malloc(3 * (image->width + 2) * sizeof(short));
if (!err || !nerr) {
NFREE(err);
NFREE(nerr);
@@ -709,8 +707,8 @@ static RXImage *image2StandardPseudoColor(RContext * ctx, RImage * image)
ofs += ximg->image->bytes_per_line - image->width;
}
wfree(err);
wfree(nerr);
free(err);
free(nerr);
}
ximg->image->data = (char *)data;
@@ -775,8 +773,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 *)wmalloc((image->width + 2) * sizeof(short));
ngerr = (short *)wmalloc((image->width + 2) * sizeof(short));
gerr = (short *)malloc((image->width + 2) * sizeof(short));
ngerr = (short *)malloc((image->width + 2) * sizeof(short));
if (!gerr || !ngerr) {
NFREE(gerr);
NFREE(ngerr);
@@ -832,8 +830,8 @@ static RXImage *image2GrayScale(RContext * ctx, RImage * image)
gerr = ngerr;
ngerr = terr;
}
wfree(gerr);
wfree(ngerr);
free(gerr);
free(ngerr);
}
ximg->image->data = (char *)data;
+2 -4
View File
@@ -26,8 +26,6 @@
#include <string.h>
#include <X11/Xlib.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "wr_i18n.h"
@@ -48,7 +46,7 @@ int RBlurImage(RImage * image)
unsigned char *pptr = NULL, *tmpp;
int ch = image->format == RRGBAFormat ? 4 : 3;
pptr = wmalloc(image->width * ch);
pptr = malloc(image->width * ch);
if (!pptr) {
RErrorCode = RERR_NOMEMORY;
return False;
@@ -140,7 +138,7 @@ int RBlurImage(RImage * image)
}
}
wfree(tmpp);
free(tmpp);
return True;
}
+9 -9
View File
@@ -34,8 +34,6 @@
#include <time.h>
#include <assert.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -127,7 +125,7 @@ static void init_cache(void)
RImageCacheMaxImage = IMAGE_CACHE_MAXIMUM_MAXPIXELS;
if (RImageCacheSize > 0) {
RImageCache = wmalloc(sizeof(RCachedImage) * RImageCacheSize);
RImageCache = malloc(sizeof(RCachedImage) * RImageCacheSize);
if (RImageCache == NULL) {
fprintf(stderr, _("wrlib: out of memory for image cache\n"));
return;
@@ -144,10 +142,10 @@ void RReleaseCache(void)
for (i = 0; i < RImageCacheSize; i++) {
if (RImageCache[i].file) {
RReleaseImage(RImageCache[i].image);
wfree(RImageCache[i].file);
free(RImageCache[i].file);
}
}
wfree(RImageCache);
free(RImageCache);
RImageCache = NULL;
RImageCacheSize = -1;
}
@@ -175,7 +173,7 @@ RImage *RLoadImage(RContext *context, const char *file, int index)
return RCloneImage(RImageCache[i].image);
} else {
wfree(RImageCache[i].file);
free(RImageCache[i].file);
RImageCache[i].file = NULL;
RReleaseImage(RImageCache[i].image);
}
@@ -256,7 +254,8 @@ RImage *RLoadImage(RContext *context, const char *file, int index)
for (i = 0; i < RImageCacheSize; i++) {
if (!RImageCache[i].file) {
RImageCache[i].file = wstrdup(file);
RImageCache[i].file = malloc(strlen(file) + 1);
strcpy(RImageCache[i].file, file);
RImageCache[i].image = RCloneImage(image);
RImageCache[i].last_modif = st.st_mtime;
RImageCache[i].last_use = time(NULL);
@@ -272,9 +271,10 @@ RImage *RLoadImage(RContext *context, const char *file, int index)
/* if no slot available, dump least recently used one */
if (!done) {
wfree(RImageCache[oldest_idx].file);
free(RImageCache[oldest_idx].file);
RReleaseImage(RImageCache[oldest_idx].image);
RImageCache[oldest_idx].file = wstrdup(file);
RImageCache[oldest_idx].file = malloc(strlen(file) + 1);
strcpy(RImageCache[oldest_idx].file, file);
RImageCache[oldest_idx].image = RCloneImage(image);
RImageCache[oldest_idx].last_modif = st.st_mtime;
RImageCache[oldest_idx].last_use = time(NULL);
+2 -4
View File
@@ -28,8 +28,6 @@
#include <gif_lib.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -129,7 +127,7 @@ RImage *RLoadGIF(const char *file, int index)
}
}
buffer = wmalloc(width * sizeof(GifPixelType));
buffer = malloc(width * sizeof(GifPixelType));
if (!buffer) {
RErrorCode = RERR_NOMEMORY;
goto bye;
@@ -221,7 +219,7 @@ RImage *RLoadGIF(const char *file, int index)
did_not_get_any_errors:
if (buffer)
wfree(buffer);
free(buffer);
if (gif)
#if (USE_GIF == 5) && (GIFLIB_MINOR >= 1)
+2 -4
View File
@@ -35,8 +35,6 @@
#include <stdnoreturn.h>
#endif
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -121,7 +119,7 @@ static RImage *do_read_jpeg_file(struct jpeg_decompress_struct *cinfo, const cha
goto abort_and_release_resources;
}
buffer[0] = (JSAMPROW) wmalloc(cinfo->image_width * cinfo->num_components);
buffer[0] = (JSAMPROW) malloc(cinfo->image_width * cinfo->num_components);
if (!buffer[0]) {
RErrorCode = RERR_NOMEMORY;
goto abort_and_release_resources;
@@ -169,7 +167,7 @@ static RImage *do_read_jpeg_file(struct jpeg_decompress_struct *cinfo, const cha
jpeg_destroy_decompress(cinfo);
fclose(file);
if (buffer[0])
wfree(buffer[0]);
free(buffer[0]);
return image;
}
+6 -8
View File
@@ -28,8 +28,6 @@
#include <png.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -167,7 +165,7 @@ RImage *RLoadPNG(RContext *context, const char *file)
image->background.blue = bkcolor->blue >> 8;
}
png_rows = wmalloc(height * sizeof(png_bytep));
png_rows = calloc(height, sizeof(png_bytep));
if (!png_rows) {
RErrorCode = RERR_NOMEMORY;
fclose(f);
@@ -176,7 +174,7 @@ RImage *RLoadPNG(RContext *context, const char *file)
return NULL;
}
for (y = 0; y < height; y++) {
png_rows[y] = wmalloc(png_get_rowbytes(png, pinfo));
png_rows[y] = malloc(png_get_rowbytes(png, pinfo));
if (!png_rows[y]) {
RErrorCode = RERR_NOMEMORY;
fclose(f);
@@ -184,8 +182,8 @@ RImage *RLoadPNG(RContext *context, const char *file)
png_destroy_read_struct(&png, &pinfo, &einfo);
while (y-- > 0)
if (png_rows[y])
wfree(png_rows[y]);
wfree(png_rows);
free(png_rows[y]);
free(png_rows);
return NULL;
}
}
@@ -216,7 +214,7 @@ RImage *RLoadPNG(RContext *context, const char *file)
}
for (y = 0; y < height; y++)
if (png_rows[y])
wfree(png_rows[y]);
wfree(png_rows);
free(png_rows[y]);
free(png_rows);
return image;
}
+3 -5
View File
@@ -29,8 +29,6 @@
#include <string.h>
#include <limits.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -163,7 +161,7 @@ static RImage *load_graymap(FILE *file, int w, int h, int max, int raw, const ch
if (raw == '5') {
char *buf;
buf = wmalloc(w + 1);
buf = malloc(w + 1);
if (!buf) {
RErrorCode = RERR_NOMEMORY;
RReleaseImage(image);
@@ -171,7 +169,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)) {
wfree(buf);
free(buf);
RErrorCode = RERR_BADIMAGEFILE;
RReleaseImage(image);
return NULL;
@@ -183,7 +181,7 @@ static RImage *load_graymap(FILE *file, int w, int h, int max, int raw, const ch
*(ptr++) = buf[x];
}
}
wfree(buf);
free(buf);
}
}
}
+6 -8
View File
@@ -29,8 +29,6 @@
#include <webp/decode.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -112,7 +110,7 @@ RImage *RLoadWEBP(const char *file_name)
return NULL;
}
raw_data = (uint8_t *) wmalloc(raw_data_size);
raw_data = (uint8_t *) malloc(raw_data_size);
if (!raw_data) {
RErrorCode = RERR_NOMEMORY;
@@ -126,7 +124,7 @@ RImage *RLoadWEBP(const char *file_name)
if (r != raw_data_size) {
RErrorCode = RERR_READ;
wfree(raw_data);
free(raw_data);
return NULL;
}
@@ -135,7 +133,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;
wfree(raw_data);
free(raw_data);
return NULL;
}
@@ -143,7 +141,7 @@ RImage *RLoadWEBP(const char *file_name)
image = RCreateImage(features.width, features.height, True);
if (!image) {
RErrorCode = RERR_NOMEMORY;
wfree(raw_data);
free(raw_data);
return NULL;
}
ret = WebPDecodeRGBAInto(raw_data, raw_data_size, image->data,
@@ -153,7 +151,7 @@ RImage *RLoadWEBP(const char *file_name)
image = RCreateImage(features.width, features.height, False);
if (!image) {
RErrorCode = RERR_NOMEMORY;
wfree(raw_data);
free(raw_data);
return NULL;
}
ret = WebPDecodeRGBInto(raw_data, raw_data_size, image->data,
@@ -161,7 +159,7 @@ RImage *RLoadWEBP(const char *file_name)
features.width * 3);
}
wfree(raw_data);
free(raw_data);
if (!ret) {
fprintf(stderr, _("wrlib: failed to decode WebP from file \"%s\"\n"), file_name);
+3 -5
View File
@@ -29,8 +29,6 @@
#include <string.h>
#include <X11/xpm.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -61,11 +59,11 @@ static RImage *create_rimage_from_xpm(RContext *context, XpmImage xpm)
/* make color table */
for (i = 0; i < 4; i++) {
color_table[i] = wmalloc(xpm.ncolors * sizeof(unsigned char));
color_table[i] = malloc(xpm.ncolors * sizeof(unsigned char));
if (!color_table[i]) {
for (i = i - 1; i >= 0; i--) {
if (color_table[i])
wfree(color_table[i]);
free(color_table[i]);
}
RReleaseImage(image);
RErrorCode = RERR_NOMEMORY;
@@ -125,7 +123,7 @@ static RImage *create_rimage_from_xpm(RContext *context, XpmImage xpm)
*(data++) = color_table[3][*p];
}
for (i = 0; i < 4; i++)
wfree(color_table[i]);
free(color_table[i]);
return image;
}
+21 -21
View File
@@ -59,15 +59,15 @@ static void free_color_symbol_table(unsigned char *color_table[],
unsigned short *symbol_table)
{
if (color_table[0])
wfree(color_table[0]);
free(color_table[0]);
if (color_table[1])
wfree(color_table[1]);
free(color_table[1]);
if (color_table[2])
wfree(color_table[2]);
free(color_table[2]);
if (color_table[3])
wfree(color_table[3]);
free(color_table[3]);
if (symbol_table)
wfree(symbol_table);
free(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] = wmalloc(ccount);
color_table[1] = wmalloc(ccount);
color_table[2] = wmalloc(ccount);
color_table[3] = wmalloc(ccount);
symbol_table = wmalloc(ccount * sizeof(unsigned short));
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));
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] = wmalloc(ccount);
color_table[1] = wmalloc(ccount);
color_table[2] = wmalloc(ccount);
color_table[3] = wmalloc(ccount);
symbol_table = wmalloc(ccount * sizeof(unsigned short));
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));
bsize = csize * w + 16;
buffer = wmalloc(bsize);
buffer = malloc(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)
wfree(buffer);
free(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)
wfree(buffer);
free(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)
wfree(buffer);
free(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)
wfree(buffer);
free(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)
wfree(buffer);
free(buffer);
if (image)
RReleaseImage(image);
return NULL;
+5 -7
View File
@@ -27,8 +27,6 @@
#include <string.h>
#include <X11/Xlib.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "wr_i18n.h"
@@ -55,7 +53,7 @@ RImage *RCreateImage(unsigned width, unsigned height, int alpha)
return NULL;
}
image = wmalloc(sizeof(RImage));
image = malloc(sizeof(RImage));
if (!image) {
RErrorCode = RERR_NOMEMORY;
return NULL;
@@ -70,10 +68,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 = wmalloc(width * height * (alpha ? 4 : 3) + 4);
image->data = malloc(width * height * (alpha ? 4 : 3) + 4);
if (!image->data) {
RErrorCode = RERR_NOMEMORY;
wfree(image);
free(image);
image = NULL;
}
@@ -96,8 +94,8 @@ void RReleaseImage(RImage * image)
image->refCount--;
if (image->refCount < 1) {
wfree(image->data);
wfree(image);
free(image->data);
free(image);
}
}
+2 -4
View File
@@ -29,8 +29,6 @@
#include <errno.h>
#include <jpeglib.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -61,7 +59,7 @@ Bool RSaveJPEG(RImage *img, const char *filename, char *title)
img_depth = 3;
/* collect separate RGB values to a buffer */
buffer = wmalloc(sizeof(char) * 3 * img->width * img->height);
buffer = malloc(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);
@@ -99,7 +97,7 @@ Bool RSaveJPEG(RImage *img, const char *filename, char *title)
jpeg_finish_compress(&cinfo);
/* Clean */
wfree(buffer);
free(buffer);
fclose(file);
return True;
+2 -4
View File
@@ -29,8 +29,6 @@
#include <errno.h>
#include <png.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -97,7 +95,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) wmalloc(3 * width * sizeof(png_byte));
png_row = (png_bytep) malloc(3 * width * sizeof(png_byte));
/* Write image data */
for (y = 0; y < height; y++) {
@@ -123,7 +121,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)
wfree(png_row);
free(png_row);
return True;
}
+2 -4
View File
@@ -28,8 +28,6 @@
#include <assert.h>
#include <errno.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
@@ -97,7 +95,7 @@ static Bool addcolor(XPMColor ** list, unsigned r, unsigned g, unsigned b, int *
if (tmpc)
return True;
newc = wmalloc(sizeof(XPMColor));
newc = malloc(sizeof(XPMColor));
if (!newc) {
@@ -151,7 +149,7 @@ static void freecolormap(XPMColor * colormap)
while (colormap) {
tmp = colormap->next;
wfree(colormap);
free(colormap);
colormap = tmp;
}
}
+13 -15
View File
@@ -29,8 +29,6 @@
#include <math.h>
#include <assert.h>
#include <WINGs/WUtil.h>
#include "wraster.h"
#include "scale.h"
#include "wr_i18n.h"
@@ -297,7 +295,7 @@ typedef struct {
/* clamp the input to the specified range */
#define CLAMP(v,l,h) ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
/* return of wmalloc is not checked if NULL in the function below! */
/* return of calloc 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 */
@@ -321,13 +319,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 *) wmalloc(new_width * sizeof(CLIST));
contrib = (CLIST *) calloc(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 *) wmalloc(ceil(width * 2 + 1) * sizeof(CONTRIB));
contrib[i].p = (CONTRIB *) calloc((int) ceil(width * 2 + 1), sizeof(CONTRIB));
center = (double)i / xscale;
left = ceil(center - width);
right = floor(center + width);
@@ -350,7 +348,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 *) wmalloc(ceil(fwidth * 2 + 1) * sizeof(CONTRIB));
contrib[i].p = (CONTRIB *) calloc((int) ceil(fwidth * 2 + 1), sizeof(CONTRIB));
center = (double)i / xscale;
left = ceil(center - fwidth);
right = floor(center + fwidth);
@@ -397,18 +395,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) {
wfree(contrib[i].p);
free(contrib[i].p);
}
wfree(contrib);
free(contrib);
/* pre-calculate filter contributions for a column */
contrib = (CLIST *) wmalloc(dst->height * sizeof(CLIST));
contrib = (CLIST *) calloc(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 *) wmalloc(ceil(width * 2 + 1) * sizeof(CONTRIB));
contrib[i].p = (CONTRIB *) calloc((int) ceil(width * 2 + 1), sizeof(CONTRIB));
center = (double)i / yscale;
left = ceil(center - width);
right = floor(center + width);
@@ -430,7 +428,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 *) wmalloc(ceil(fwidth * 2 + 1) * sizeof(CONTRIB));
contrib[i].p = (CONTRIB *) calloc((int) ceil(fwidth * 2 + 1), sizeof(CONTRIB));
center = (double)i / yscale;
left = ceil(center - fwidth);
right = floor(center + fwidth);
@@ -452,7 +450,7 @@ RImage *RSmoothScaleImage(RImage * src, unsigned new_width, unsigned new_height)
}
/* apply filter to zoom vertically from tmp to dst */
sp = wmalloc(tmp->height * 3);
sp = malloc(tmp->height * 3);
for (k = 0; k < new_width; ++k) {
CONTRIB *pp;
@@ -487,13 +485,13 @@ RImage *RSmoothScaleImage(RImage * src, unsigned new_width, unsigned new_height)
p += new_width * 3;
}
}
wfree(sp);
free(sp);
/* free the memory allocated for vertical filter weights */
for (i = 0; i < dst->height; ++i) {
wfree(contrib[i].p);
free(contrib[i].p);
}
wfree(contrib);
free(contrib);
RReleaseImage(tmp);
+6 -6
View File
@@ -38,7 +38,7 @@ int main(int argc, char **argv)
else
ProgName++;
color_name = (char **)wmalloc(sizeof(char *) * argc);
color_name = (char **)malloc(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 = wmalloc(sizeof(RColor *) * (ncolors + 1));
colors = malloc(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] = wmalloc(sizeof(RColor));
colors[i] = malloc(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();
wfree(color_name);
free(color_name);
for (i = 0; i < ncolors + 1; i++)
wfree(colors[i]);
wfree(colors);
free(colors[i]);
free(colors);
RDestroyContext(ctx);
RShutdown();
+11 -13
View File
@@ -31,8 +31,6 @@
#include <assert.h>
#include <WINGs/WUtil.h>
#ifdef USE_XSHM
#include <sys/ipc.h>
#include <sys/shm.h>
@@ -65,7 +63,7 @@ RXImage *RCreateXImage(RContext * context, int depth, unsigned width, unsigned h
RXImage *rximg;
Visual *visual = context->visual;
rximg = wmalloc(sizeof(RXImage));
rximg = malloc(sizeof(RXImage));
if (!rximg) {
RErrorCode = RERR_NOMEMORY;
return NULL;
@@ -73,14 +71,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) {
wfree(rximg);
free(rximg);
RErrorCode = RERR_XERROR;
return NULL;
}
rximg->image->data = wmalloc(rximg->image->bytes_per_line * height);
rximg->image->data = malloc(rximg->image->bytes_per_line * height);
if (!rximg->image->data) {
XDestroyImage(rximg->image);
wfree(rximg);
free(rximg);
RErrorCode = RERR_NOMEMORY;
return NULL;
}
@@ -92,14 +90,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) {
wfree(rximg);
free(rximg);
RErrorCode = RERR_XERROR;
return NULL;
}
rximg->image->data = wmalloc(rximg->image->bytes_per_line * height);
rximg->image->data = malloc(rximg->image->bytes_per_line * height);
if (!rximg->image->data) {
XDestroyImage(rximg->image);
wfree(rximg);
free(rximg);
RErrorCode = RERR_NOMEMORY;
return NULL;
}
@@ -175,7 +173,7 @@ void RDestroyXImage(RContext * context, RXImage * rximage)
XDestroyImage(rximage->image);
}
#endif
wfree(rximage);
free(rximage);
}
static unsigned getDepth(Display * dpy, Drawable d)
@@ -207,7 +205,7 @@ RXImage *RGetXImage(RContext * context, Drawable d, int x, int y, unsigned width
}
}
if (!ximg) {
ximg = wmalloc(sizeof(RXImage));
ximg = malloc(sizeof(RXImage));
if (!ximg) {
RErrorCode = RERR_NOMEMORY;
return NULL;
@@ -216,7 +214,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 = wmalloc(sizeof(RXImage));
ximg = malloc(sizeof(RXImage));
if (!ximg) {
RErrorCode = RERR_NOMEMORY;
return NULL;
@@ -226,7 +224,7 @@ RXImage *RGetXImage(RContext * context, Drawable d, int x, int y, unsigned width
#endif /* !USE_XSHM */
if (ximg->image == NULL) {
wfree(ximg);
free(ximg);
return NULL;
}
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "wutil-rs"
version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["staticlib"]
+22
View File
@@ -0,0 +1,22 @@
AUTOMAKE_OPTIONS =
RUST_SOURCES = \
src/array.rs \
src/find_file.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
+449
View File
@@ -0,0 +1,449 @@
use std::{ffi::c_void, ptr::NonNull};
pub struct Array {
items: Vec<NonNull<c_void>>,
destructor: Option<unsafe extern "C" fn(x: *mut c_void)>,
}
pub mod ffi {
use super::Array;
use std::{
ffi::{c_int, c_void},
ptr::{self, NonNull},
};
pub const NOT_FOUND: c_int = -1;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMCreateArray(initial_size: c_int) -> *mut Array {
let cap = if initial_size < 0 {
0
} else {
initial_size as usize
};
Box::leak(Box::new(Array {
items: Vec::with_capacity(cap),
destructor: None,
}))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMCreateArrayWithDestructor(
initial_size: c_int,
destructor: unsafe extern "C" fn(x: *mut c_void),
) -> *mut Array {
let cap = if initial_size < 0 {
0
} else {
initial_size as usize
};
Box::leak(Box::new(Array {
items: Vec::with_capacity(cap),
destructor: Some(destructor),
}))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMCreateArrayWithArray(array: *mut Array) -> *mut Array {
if array.is_null() {
return ptr::null_mut();
}
let array = unsafe { &*array };
Box::leak(Box::new(Array {
items: array.items.clone(),
destructor: array.destructor,
}))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMEmptyArray(array: *mut Array) {
if array.is_null() {
return;
}
let array = unsafe { &mut *array };
if let Some(f) = array.destructor {
for item in &mut array.items {
unsafe { (f)(item.as_ptr()) }
}
}
array.items.clear();
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMFreeArray(array: *mut Array) {
if array.is_null() {
return;
}
unsafe {
WMEmptyArray(array);
let _ = ptr::read(array);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMGetArrayItemCount(array: *mut Array) -> c_int {
if array.is_null() {
return 0;
}
unsafe { (*array).items.len() as c_int }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMAddToArray(array: *mut Array, item: *mut c_void) {
if array.is_null() {
return;
}
if let Some(item) = NonNull::new(item) {
unsafe {
(*array).items.push(item);
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMInsertInArray(array: *mut Array, index: c_int, item: *mut c_void) {
if array.is_null() {
return;
}
if index < 0 {
return;
}
let array = unsafe { &mut (*array).items };
let index = index as usize;
if index >= array.len() {
return;
}
if let Some(item) = NonNull::new(item) {
array.insert(index, item);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMSetInArray(
array: *mut Array,
index: c_int,
item: *mut c_void,
) -> *mut c_void {
if array.is_null() {
return ptr::null_mut();
}
if index < 0 {
return ptr::null_mut();
}
let index = index as usize;
/* is it really useful to perform append if index == array->itemCount ? -Dan */
if index == unsafe { (*array).items.len() } {
unsafe {
WMAddToArray(array, item);
}
return ptr::null_mut();
}
let item = match NonNull::new(item) {
Some(x) => x,
None => return ptr::null_mut(),
};
let array = unsafe { &mut (*array).items };
let old = array[index];
array[index] = item;
old.as_ptr()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMDeleteFromArray(array: *mut Array, index: c_int) -> c_int {
if array.is_null() {
return 0;
}
let array = unsafe { &mut *array };
if index < 0 {
return 0;
}
let index = index as usize;
if index >= array.items.len() {
0
} else {
let old = array.items.remove(index);
if let Some(f) = array.destructor {
unsafe {
(f)(old.as_ptr());
}
}
1
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMRemoveFromArray(array: *mut Array, item: *mut c_void) -> c_int {
unsafe { WMRemoveFromArrayMatching(array, None, item) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMRemoveFromArrayMatching(
array: *mut Array,
pred: Option<unsafe extern "C" fn(item: *const c_void, cdata: *mut c_void) -> c_int>,
cdata: *mut c_void,
) -> c_int {
if array.is_null() {
return 1;
}
let array = unsafe { &mut *array };
let original_len = array.items.len();
match pred {
Some(f) => array.items.retain(|x| unsafe { f(x.as_ptr(), cdata) != 0 }),
None => array.items.retain(|x| ptr::eq(x.as_ptr(), cdata)),
}
(original_len - array.items.len()) as c_int
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMGetFromArray(array: *mut Array, index: c_int) -> *mut c_void {
if array.is_null() || index < 0 {
return ptr::null_mut();
}
unsafe {
(*array)
.items
.get(index as usize)
.map(|p| p.as_ptr())
.unwrap_or(ptr::null_mut())
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMGetFirstInArray(array: *mut Array, item: *mut c_void) -> c_int {
unsafe { WMFindInArray(array, None, item) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMPopFromArray(array: *mut Array) -> *mut c_void {
if array.is_null() {
return ptr::null_mut();
}
unsafe {
(*array)
.items
.pop()
.map(|p| p.as_ptr())
.unwrap_or(ptr::null_mut())
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMFindInArray(
array: *mut Array,
pred: Option<unsafe extern "C" fn(item: *const c_void, cdata: *mut c_void) -> c_int>,
cdata: *mut c_void,
) -> c_int {
if array.is_null() {
return NOT_FOUND;
}
let array = unsafe { &*array };
if let Some(f) = pred {
array
.items
.iter()
.enumerate()
.find(|(_, item)| unsafe { f(item.as_ptr(), cdata) != 0 })
.map(|(i, _)| i as c_int)
.unwrap_or(NOT_FOUND)
} else {
array
.items
.iter()
.enumerate()
.find(|(_, item)| ptr::eq(item.as_ptr(), cdata))
.map(|(i, _)| i as c_int)
.unwrap_or(NOT_FOUND)
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMCountInArray(array: *mut Array, item: *const c_void) -> c_int {
if array.is_null() {
return 0;
}
let array = unsafe { &*array };
array
.items
.iter()
.filter(|x| ptr::eq(x.as_ptr(), item))
.count() as c_int
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMSortArray(
array: *mut Array,
comparator: unsafe extern "C" fn(a: *const c_void, b: *const c_void) -> c_int,
) {
if array.is_null() {
return;
}
unsafe {
(*array)
.items
.sort_by(|&a, &b| match comparator(a.as_ptr(), b.as_ptr()).signum() {
-1 => std::cmp::Ordering::Less,
0 => std::cmp::Ordering::Equal,
1 => std::cmp::Ordering::Greater,
_ => unreachable!(),
})
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMMapArray(
array: *mut Array,
f: unsafe extern "C" fn(*mut c_void, *mut c_void),
data: *mut c_void,
) {
if array.is_null() {
return;
}
unsafe {
for a in &mut (*array).items {
(f)(a.as_ptr(), data);
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMArrayFirst(array: *mut Array, iter: *mut c_int) -> *mut c_void {
if array.is_null() || iter.is_null() {
return ptr::null_mut();
}
let array = unsafe { &*array };
match array.items.get(0) {
None => {
unsafe {
*iter = NOT_FOUND;
}
ptr::null_mut()
}
Some(x) => {
unsafe {
*iter = 0;
}
x.as_ptr()
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMArrayLast(array: *mut Array, iter: *mut c_int) -> *mut c_void {
if array.is_null() || iter.is_null() {
return ptr::null_mut();
}
let array = unsafe { &*array };
match array.items.last() {
None => {
unsafe {
*iter = NOT_FOUND;
}
ptr::null_mut()
}
Some(x) => {
unsafe {
*iter = (array.items.len() - 1) as c_int;
}
x.as_ptr()
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMArrayNext(array: *mut Array, iter: *mut c_int) -> *mut c_void {
if array.is_null() || iter.is_null() {
return ptr::null_mut();
}
let array = unsafe { &*array };
let index = unsafe { *iter };
if index < 0 {
return ptr::null_mut();
}
match array.items.get(index as usize) {
Some(i) => {
unsafe {
*iter += 1;
}
i.as_ptr()
}
None => {
unsafe {
*iter = NOT_FOUND;
}
ptr::null_mut()
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMArrayPrevious(array: *mut Array, iter: *mut c_int) -> *mut c_void {
if array.is_null() || iter.is_null() {
return ptr::null_mut();
}
let array = unsafe { &*array };
let index = unsafe { *iter };
if index < 0 {
return ptr::null_mut();
}
match array.items.get(index as usize) {
Some(i) => {
unsafe {
*iter -= 1;
}
i.as_ptr()
}
None => {
unsafe {
*iter = NOT_FOUND;
}
ptr::null_mut()
}
}
}
}
#[cfg(test)]
mod test {
use std::{ffi::c_void, ptr};
use super::ffi::*;
#[test]
fn create_destroy_with_size() {
unsafe {
let array = WMCreateArray(10);
assert_eq!((*array).items.len(), 0);
assert!((*array).items.capacity() >= 10);
WMFreeArray(array);
}
}
#[test]
fn create_push_clear_destroy() {
static mut SENTINEL: *mut c_void = ptr::null_mut();
unsafe extern "C" fn destructor(item: *mut c_void) {
unsafe {
SENTINEL = item;
}
}
unsafe {
let array = WMCreateArrayWithDestructor(10, destructor);
assert!(SENTINEL.is_null());
let mut x = 0xdeadbeefu32;
WMAddToArray(array, (&mut x as *mut u32).cast::<c_void>());
assert_eq!(WMGetArrayItemCount(array), 1);
WMEmptyArray(array);
assert!(ptr::eq(SENTINEL, (&x as *const u32).cast::<c_void>()));
assert_eq!(0xdeadbeefu32, *SENTINEL.cast::<u32>());
SENTINEL = ptr::null_mut();
WMFreeArray(array);
assert!(SENTINEL.is_null());
}
}
}
+196
View File
@@ -0,0 +1,196 @@
//! Self-owning shared data segment.
use std::{cell::RefCell, rc::Rc};
#[derive(Clone, Copy, Debug)]
pub enum Format {
Z = 0,
E = 8,
S = 16,
T = 32,
}
/// Reference-counted, self-owned, dynamically sized chunk of bytes.
///
/// In the original WINGs, this type either owned or borrowed a data buffer and
/// had some associated metadata. In Rust, this is little more than a thin
/// wrapper around an `Rc<RefCell<Vec<u8>>>`. It is mostly used by proplists,
/// and it should be done away with once its dependents have been ported to
/// Rust.
pub struct Data(Rc<RefCell<Inner>>);
struct Inner {
bytes: Vec<u8>,
format: Format,
}
pub mod ffi {
use super::{Data, Format, Inner};
use std::{
cell::RefCell,
ffi::{c_int, c_uint, c_void},
ptr,
rc::Rc,
};
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMCreateDataWithCapacity(capacity: c_uint) -> *mut Data {
Box::leak(Box::new(Data(Rc::new(RefCell::new(Inner {
bytes: Vec::with_capacity(capacity as usize),
format: Format::Z,
})))))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMCreateDataWithLength(length: c_uint) -> *mut Data {
Box::leak(Box::new(Data(Rc::new(RefCell::new(Inner {
bytes: vec![0; length as usize],
format: Format::Z,
})))))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMCreateDataWithBytes(
bytes: *const c_void,
length: c_uint,
) -> *mut Data {
let bytes = unsafe { &*ptr::slice_from_raw_parts(bytes.cast::<u8>(), length as usize) };
let bytes = Vec::from(bytes);
Box::leak(Box::new(Data(Rc::new(RefCell::new(Inner {
bytes,
format: Format::Z,
})))))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMCreateDataWithData(data: *mut Data) -> *mut Data {
if data.is_null() {
return ptr::null_mut();
}
let data = unsafe { &*data };
Box::leak(Box::new(Data(Rc::new(RefCell::new(Inner {
bytes: data.0.borrow().bytes.clone(),
format: data.0.borrow().format,
})))))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMRetainData(data: *mut Data) -> *mut Data {
if data.is_null() {
return ptr::null_mut();
}
let data = unsafe { &*data };
Box::leak(Box::new(Data(data.0.clone())))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMReleaseData(data: *mut Data) {
if data.is_null() {
return;
}
let _ = unsafe { ptr::read(data) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMDataBytes(data: *mut Data) -> *const c_void {
if data.is_null() {
return ptr::null();
}
unsafe { (*data).0.borrow().bytes.as_ptr().cast::<c_void>() }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMIsDataEqualToData(a: *mut Data, b: *mut Data) -> c_int {
if a.is_null() || b.is_null() {
return 0;
}
if ptr::eq(a, b) {
return 1;
}
let a = unsafe { &*a };
let b = unsafe { &*b };
(a.0.borrow().bytes == b.0.borrow().bytes) as c_int
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMGetDataLength(data: *mut Data) -> c_uint {
if data.is_null() {
return 0;
}
unsafe { (*data).0.borrow().bytes.len() as c_uint }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMAppendDataBytes(
data: *mut Data,
bytes: *const c_void,
length: c_uint,
) {
if data.is_null() || bytes.is_null() || length == 0 {
return;
}
let data = unsafe { &mut *data };
let bytes = unsafe { &*ptr::slice_from_raw_parts(bytes.cast::<u8>(), length as usize) };
data.0.borrow_mut().bytes.extend_from_slice(bytes);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMAppendData(data: *mut Data, ext: *mut Data) {
if data.is_null() || ext.is_null() {
return;
}
let data = unsafe { &mut *data };
let ext = unsafe { &*ext };
data.0
.borrow_mut()
.bytes
.extend_from_slice(&ext.0.borrow().bytes);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMSetData(data: *mut Data, other: *mut Data) {
if data.is_null() || other.is_null() {
return;
}
let data = unsafe { &mut *data };
let other = unsafe { &*other };
data.0
.borrow_mut()
.bytes
.copy_from_slice(&other.0.borrow().bytes);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMGetDataFormat(data: *mut Data) -> c_uint {
if data.is_null() {
return 0;
}
return unsafe {
match (*data).0.borrow().format {
Format::Z => 0,
Format::E => 8,
Format::S => 16,
Format::T => 32,
}
};
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn WMSetDataFormat(data: *mut Data, format: c_uint) {
if data.is_null() {
return;
}
let format = match format {
0 => Format::Z,
8 => Format::E,
16 => Format::S,
32 => Format::T,
_ => return,
};
unsafe {
(*data).0.borrow_mut().format = format;
}
}
}
+221
View File
@@ -0,0 +1,221 @@
//! This module provides approximate reimplementations of file-finding routines
//! from the original WINGs utilities.
//!
//! The [`ffi`] submodule provides functions which may be called directly from C
//! that has not yet been ported to Rust.
//!
//! The original utilities expanded environment variables in path names
//! (expanding `$FOO/bar/baz` to use the value of the environment variable
//! `FOO`) and respected Unix-style denotations of user home directories
//! (resolving `~someuser/foo.txt` to `(home directory of
//! someuser)/foo.txt`. These behaviors have not been preserved. But a path
//! whose first component is `~` will still be resolved relatively to the
//! current user's home directory.
//!
//! Keep in mind that these utilities are not strictly correct as originally
//! designed: a file path that appears valid when it is checked in a subroutine
//! may become invalid if the file is deleted between when the path is checked
//! and when downstream code attempts to open the file. A better design would
//! open the file and return a live file pointer instead of simply returning a
//! path that is likely to work. Future work should redesign this module to
//! avoid this issue.
use std::{
env,
ffi::OsStr,
fs::File,
path::{Component, Path, PathBuf},
};
/// If `file` is an absolute path can be opened, returns that path. Paths
/// starting with `~` are treated as absolute, and the user's home directory is
/// substituted for `~` (so `~/foo` becomes `(users's home directory)/foo`). If
/// the user's home directory cannot be determined, `/` is used instead.
///
/// Returns `None` otherwise.
pub fn absolute(file: &Path) -> Option<PathBuf> {
if file.is_absolute() {
return Some(file.to_path_buf());
} else {
let mut components = file.components();
if components.next() == Some(Component::Normal(OsStr::new("~"))) {
let mut path = env::home_dir().unwrap_or_else(|| PathBuf::from("/"));
for c in components {
path.push(c);
}
Some(path)
} else {
None
}
}
}
/// Resolves `file` to a path that can be opened relative to an element of
/// `paths`, or `None` if it cannot be found. If `paths` is empty, an attempt to
/// resolve `file` relative to the current working directory will be made.
pub fn in_paths<'a>(paths: impl Iterator<Item = &'a Path>, file: &Path) -> Option<PathBuf> {
if file.file_name().map(|f| f.is_empty()).unwrap_or(false) {
return None;
}
let mut paths = paths.peekable();
if paths.peek().is_none() {
if let Ok(_) = File::open(file) {
return Some(file.to_path_buf());
}
return None;
}
let mut buf = PathBuf::new();
for parent in paths {
buf.clear();
buf.push(parent);
buf.push(file);
if let Ok(_) = File::open(&buf) {
return Some(buf);
}
}
None
}
pub mod ffi {
use super::{absolute, in_paths};
use crate::memory::alloc_bytes;
use std::{
env,
ffi::{CStr, OsStr, c_char, c_int},
iter,
os::unix::ffi::OsStrExt,
path::{Path, PathBuf},
ptr,
};
fn split_paths(paths: &CStr) -> impl Iterator<Item = &[u8]> {
paths.to_bytes().split(|b| *b == b':')
}
fn to_c_str(p: &Path) -> *mut c_char {
let os_bytes = p.as_os_str().as_encoded_bytes();
let buf = alloc_bytes(os_bytes.len() + 1);
unsafe {
ptr::copy_nonoverlapping(os_bytes.as_ptr(), buf, os_bytes.len());
}
buf.cast::<c_char>()
}
/// Attempts to find `file` under colon-separated `paths`. Checks if `file`
/// is absolute or prefixed with `~` before attempting to resolve it
/// relatively. If no file can be found, returns NULL. Non-NULL return
/// values must be freed with [`crate::memory::free_bytes`] or
/// [`crate::memory::ffi::wfree`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wfindfile(paths: *const c_char, file: *const c_char) -> *mut c_char {
if file.is_null() {
return ptr::null_mut();
}
let file = unsafe { CStr::from_ptr(file) };
let file = Path::new(OsStr::from_bytes(file.to_bytes()));
if let Some(path) = absolute(&file) {
return to_c_str(&path);
}
let path = if paths.is_null() {
in_paths(iter::empty(), file)
} else {
let paths = unsafe { CStr::from_ptr(paths) };
in_paths(
split_paths(paths).map(|p| Path::new(OsStr::from_bytes(p))),
file,
)
};
path.map(|x| to_c_str(x.as_ref()))
.unwrap_or(ptr::null_mut())
}
/// Attempts to find `file` under an element of NULL-terminated
/// `path_list`. Checks if `file` is absolute or prefixed with `~` before
/// attempting to resolve it relatively. If no file can be found, returns
/// NULL. Non-NULL return values must be freed with
/// [`crate::memory::free_bytes`] or [`crate::memory::ffi::wfree`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wfindfileinlist(
path_list: *const *const c_char,
file: *const c_char,
) -> *mut c_char {
if file.is_null() {
return ptr::null_mut();
}
let file = unsafe { CStr::from_ptr(file) };
let file = Path::new(OsStr::from_bytes(file.to_bytes()));
if let Some(path) = absolute(&file) {
return to_c_str(&path);
}
let path = if path_list.is_null() {
in_paths(iter::empty(), file)
} else {
let paths = (0usize..)
.map(|offset| unsafe { path_list.add(offset) })
.take_while(|&p| unsafe { !(*p).is_null() })
.map(|p| Path::new(OsStr::from_bytes(unsafe { CStr::from_ptr(*p).to_bytes() })));
in_paths(paths, file)
};
path.map(|x| to_c_str(x.as_ref()))
.unwrap_or(ptr::null_mut())
}
/// Attempts to expand `path` if it starts with `~` by replacing the first
/// path element with the user's home directory. Returns NULL if `path` is
/// NULL. Non-NULL return values must be freed with
/// [`crate::memory::free_bytes`] or [`crate::memory::ffi::wfree`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wexpandpath(path: *const c_char) -> *mut c_char {
if path.is_null() {
return ptr::null_mut();
}
let path = unsafe { CStr::from_ptr(path) };
let path = Path::new(OsStr::from_bytes(path.to_bytes()));
absolute(path)
.map(|p| to_c_str(&p))
.unwrap_or_else(|| to_c_str(&path))
}
/// Returns the home directory of the current user, or `"/"` if it cannot be
/// determined. The returned value must be freed with
/// [`crate::memory::free_bytes`] or [`crate::memory::ffi::wfree`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wgethomedir() -> *mut c_char {
match env::home_dir() {
Some(x) => to_c_str(x.as_ref()),
None => to_c_str(Path::new("/")),
}
}
/// Copies `src_file` into `dest_dir/dest_file`. Returns 0 on success, or -1
/// on error.
///
/// This is provided solely to support code that has not yet been ported to
/// Rust. Prefer `std::fs::copy` or another utility if you can.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wcopy_file(
dest_dir: *const c_char,
src_file: *const c_char,
dest_file: *const c_char,
) -> c_int {
if dest_dir.is_null() || src_file.is_null() || dest_file.is_null() {
return -1;
}
let src_file = unsafe { CStr::from_ptr(src_file) };
let dest_dir = unsafe { CStr::from_ptr(dest_dir) };
let dest_file = unsafe { CStr::from_ptr(dest_file) };
let src = Path::new(OsStr::from_bytes(src_file.to_bytes()));
let mut dest = PathBuf::from(OsStr::from_bytes(dest_dir.to_bytes()));
dest.push(OsStr::from_bytes(dest_file.to_bytes()));
if std::fs::copy(src, dest).is_ok() {
return 0;
} else {
return -1;
}
}
}
+4
View File
@@ -0,0 +1,4 @@
pub mod array;
pub mod data;
pub mod find_file;
pub mod memory;
+222
View File
@@ -0,0 +1,222 @@
//! 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>()); }
}
}