mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-20 04:48:06 +01:00
Fixed behaviour of WMData objects regarding the destructor.
Merged WMCreateDataWithBytesNoCopy with WMCreateDataWithBytesAndDestructor.
This commit is contained in:
@@ -440,12 +440,11 @@ WMData* WMCreateDataWithLength(unsigned length);
|
|||||||
|
|
||||||
WMData* WMCreateDataWithBytes(void *bytes, unsigned length);
|
WMData* WMCreateDataWithBytes(void *bytes, unsigned length);
|
||||||
|
|
||||||
WMData* WMCreateDataWithBytesNoCopy(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* WMCreateDataWithBytesAndDestructor(void *bytes, unsigned length,
|
|
||||||
WMFreeDataProc *destructor);
|
|
||||||
|
|
||||||
WMData* WMCreateDataWithData(WMData *aData);
|
WMData* WMCreateDataWithData(WMData *aData);
|
||||||
|
|
||||||
WMData* WMRetainData(WMData *aData);
|
WMData* WMRetainData(WMData *aData);
|
||||||
|
|||||||
49
WINGs/data.c
49
WINGs/data.c
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* WINGs WMData function library
|
* WINGs WMData function library
|
||||||
*
|
*
|
||||||
* Copyright (c) 1999 Dan Pascu
|
* Copyright (c) 1999-2000 Dan Pascu
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -51,12 +51,13 @@ WMCreateDataWithCapacity(unsigned capacity) /*FOLD00*/
|
|||||||
aData->bytes = wmalloc(capacity);
|
aData->bytes = wmalloc(capacity);
|
||||||
else
|
else
|
||||||
aData->bytes = NULL;
|
aData->bytes = NULL;
|
||||||
|
|
||||||
aData->capacity = capacity;
|
aData->capacity = capacity;
|
||||||
aData->growth = capacity/2 > 0 ? capacity/2 : 1;
|
aData->growth = capacity/2 > 0 ? capacity/2 : 1;
|
||||||
aData->length = 0;
|
aData->length = 0;
|
||||||
aData->retainCount = 1;
|
aData->retainCount = 1;
|
||||||
aData->freeData = 1;
|
|
||||||
aData->format = 0;
|
aData->format = 0;
|
||||||
|
aData->freeData = 1;
|
||||||
aData->destructor = NULL;
|
aData->destructor = NULL;
|
||||||
|
|
||||||
return aData;
|
return aData;
|
||||||
@@ -92,27 +93,8 @@ WMCreateDataWithBytes(void *bytes, unsigned length) /*FOLD00*/
|
|||||||
|
|
||||||
|
|
||||||
WMData*
|
WMData*
|
||||||
WMCreateDataWithBytesNoCopy(void *bytes, unsigned length) /*FOLD00*/
|
WMCreateDataWithBytesNoCopy(void *bytes, unsigned length, /*FOLD00*/
|
||||||
{
|
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->freeData = 0;
|
|
||||||
aData->format = 0;
|
|
||||||
aData->destructor = NULL;
|
|
||||||
|
|
||||||
return aData;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
WMData*
|
|
||||||
WMCreateDataWithBytesAndDestructor(void *bytes, unsigned length,
|
|
||||||
WMFreeDataProc *destructor)
|
|
||||||
{
|
{
|
||||||
WMData *aData;
|
WMData *aData;
|
||||||
|
|
||||||
@@ -134,12 +116,12 @@ WMData*
|
|||||||
WMCreateDataWithData(WMData *aData) /*FOLD00*/
|
WMCreateDataWithData(WMData *aData) /*FOLD00*/
|
||||||
{
|
{
|
||||||
WMData *newData;
|
WMData *newData;
|
||||||
|
|
||||||
if (aData->length > 0) {
|
if (aData->length > 0) {
|
||||||
newData = WMCreateDataWithBytes(aData->bytes, aData->length);
|
newData = WMCreateDataWithBytes(aData->bytes, aData->length);
|
||||||
} else {
|
} else {
|
||||||
newData = WMCreateDataWithCapacity(0);
|
newData = WMCreateDataWithCapacity(0);
|
||||||
}
|
}
|
||||||
newData->destructor = aData->destructor;
|
|
||||||
newData->format = aData->format;
|
newData->format = aData->format;
|
||||||
|
|
||||||
return newData;
|
return newData;
|
||||||
@@ -160,11 +142,12 @@ WMReleaseData(WMData *aData) /*FOLD00*/
|
|||||||
aData->retainCount--;
|
aData->retainCount--;
|
||||||
if (aData->retainCount > 0)
|
if (aData->retainCount > 0)
|
||||||
return;
|
return;
|
||||||
if (aData->bytes && aData->freeData) {
|
if (aData->bytes != NULL) {
|
||||||
if (aData->destructor != NULL)
|
if (aData->destructor != NULL) {
|
||||||
aData->destructor(aData->bytes);
|
aData->destructor(aData->bytes);
|
||||||
else
|
} else if (aData->freeData) {
|
||||||
wfree(aData->bytes);
|
wfree(aData->bytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
wfree(aData);
|
wfree(aData);
|
||||||
}
|
}
|
||||||
@@ -254,7 +237,7 @@ WMGetDataBytesWithLength(WMData *aData, void *buffer, unsigned length) /*FOLD00*
|
|||||||
void
|
void
|
||||||
WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange) /*FOLD00*/
|
WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange) /*FOLD00*/
|
||||||
{
|
{
|
||||||
unsigned char *dataBytes = (unsigned char *)aData->bytes;
|
unsigned char *dataBytes = (unsigned char *)aData->bytes;
|
||||||
|
|
||||||
wassertr(aRange.position < aData->length);
|
wassertr(aRange.position < aData->length);
|
||||||
wassertr(aRange.count <= aData->length-aRange.position);
|
wassertr(aRange.count <= aData->length-aRange.position);
|
||||||
@@ -274,8 +257,8 @@ WMGetSubdataWithRange(WMData *aData, WMRange aRange) /*FOLD00*/
|
|||||||
|
|
||||||
buffer = wmalloc(aRange.count);
|
buffer = wmalloc(aRange.count);
|
||||||
WMGetDataBytesWithRange(aData, buffer, aRange);
|
WMGetDataBytesWithRange(aData, buffer, aRange);
|
||||||
newData = WMCreateDataWithBytesNoCopy(buffer, aRange.count);
|
newData = WMCreateDataWithBytesNoCopy(buffer, aRange.count, wfree);
|
||||||
newData->destructor = aData->destructor;
|
//newData->freeData = 1;
|
||||||
newData->format = aData->format;
|
newData->format = aData->format;
|
||||||
|
|
||||||
return newData;
|
return newData;
|
||||||
@@ -310,7 +293,7 @@ WMAppendDataBytes(WMData *aData, void *bytes, unsigned length) /*FOLD00*/
|
|||||||
{
|
{
|
||||||
unsigned oldLength = aData->length;
|
unsigned oldLength = aData->length;
|
||||||
unsigned newLength = oldLength + length;
|
unsigned newLength = oldLength + length;
|
||||||
unsigned char *dataBytes = (unsigned char *)aData->bytes;
|
unsigned char *dataBytes = (unsigned char *)aData->bytes;
|
||||||
|
|
||||||
if (newLength > aData->capacity) {
|
if (newLength > aData->capacity) {
|
||||||
unsigned nextCapacity = aData->capacity + aData->growth;
|
unsigned nextCapacity = aData->capacity + aData->growth;
|
||||||
|
|||||||
@@ -330,8 +330,7 @@ getSelectionData(Display *dpy, Window win, Atom where)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
wdata = WMCreateDataWithBytesAndDestructor(data, len,
|
wdata = WMCreateDataWithBytesNoCopy(data, len, (WMFreeDataProc*)XFree);
|
||||||
(WMFreeDataProc*)XFree);
|
|
||||||
if (wdata == NULL) {
|
if (wdata == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user