Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit fa1b262

Browse files
Buffer free list with less reallocations
1 parent 625cc34 commit fa1b262

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

kernel/alloc.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ void
2222
CArrayDescriptor_FREE(CArrayDescriptor * descr)
2323
{
2424
if (descr->refcount < 0) {
25-
efree(descr->f);
25+
if (descr->f != NULL) {
26+
efree(descr->f);
27+
descr->f = NULL;
28+
}
29+
2630
efree(descr);
2731
descr = NULL;
2832
}

kernel/carray.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ CArray_DescrFromType(int typenum)
816816
ret->type_num = typenum;
817817
ret->numElements = 0;
818818

819+
819820
if(typenum == 0) {
820821
ret->elsize = sizeof(int);
821822
ret->type = TYPE_DEFAULT;

kernel/interfaces/rubix.c

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <kernel/buffer.h>
1616
#include <kernel/search.h>
1717
#include <kernel/round.h>
18+
#include <kernel/ctors.h>
1819
#include <kernel/clip.h>
1920
#include <kernel/calculation.h>
2021
#include <kernel/carray.h>
@@ -1175,6 +1176,8 @@ PHP_METHOD(CRubix, pow)
11751176
*/
11761177
PHP_METHOD(CRubix, mod)
11771178
{
1179+
int i;
1180+
char order = 'C';
11781181
MemoryPointer target1_ptr, target2_ptr, result_ptr;
11791182
zval * target1, * target2;
11801183
CArray * target_ca1, * target_ca2, * output_ca, * out;
@@ -1186,8 +1189,21 @@ PHP_METHOD(CRubix, mod)
11861189
ZVAL_TO_MEMORYPOINTER(target2, &target2_ptr, NULL);
11871190
target_ca1 = CArray_FromMemoryPointer(&target1_ptr);
11881191
target_ca2 = CArray_FromMemoryPointer(&target2_ptr);
1189-
output_ca = CArray_Mod(target_ca1, target_ca2, &result_ptr);
11901192

1193+
if (CArray_NDIM(target_ca1) == 1) {
1194+
if (CArray_NDIM(target_ca1) == CArray_NDIM(target_ca2)) {
1195+
CArray *rtn_ca = CArray_Zeros(CArray_DIMS(target_ca1), 1, TYPE_INTEGER, &order, &result_ptr);
1196+
for (i = 0; i < CArray_DESCR(target_ca1)->numElements; i++) {
1197+
IDATA(rtn_ca)[i] = fmod(DDATA(target_ca1)[i], DDATA(target_ca2)[i]);
1198+
}
1199+
FREE_FROM_MEMORYPOINTER(&target1_ptr);
1200+
FREE_FROM_MEMORYPOINTER(&target2_ptr);
1201+
RETURN_MEMORYPOINTER(return_value, &result_ptr);
1202+
return;
1203+
}
1204+
1205+
}
1206+
output_ca = CArray_Mod(target_ca1, target_ca2, &result_ptr);
11911207

11921208
FREE_FROM_MEMORYPOINTER(&target1_ptr);
11931209
FREE_FROM_MEMORYPOINTER(&target2_ptr);
@@ -1783,7 +1799,7 @@ PHP_METHOD(CRubix, max)
17831799
}
17841800
CArrayIterator_NEXT(it);
17851801
} while (CArrayIterator_NOTDONE(it));
1786-
1802+
CArrayIterator_FREE(it);
17871803
case TYPE_DOUBLE_INT:
17881804
i = 0, j = 0;
17891805
newshape = emalloc(sizeof(int));
@@ -1805,6 +1821,7 @@ PHP_METHOD(CRubix, max)
18051821
}
18061822
CArrayIterator_NEXT(it);
18071823
} while (CArrayIterator_NOTDONE(it));
1824+
CArrayIterator_FREE(it);
18081825
}
18091826

18101827
add_to_buffer(&rtn_ptr, rtn_ca, sizeof(CArray));
@@ -3521,14 +3538,54 @@ PHP_METHOD(CRubix, count)
35213538

35223539
}
35233540

3524-
/**
3525-
* Rubix/Tensor/Matrix::offsetSet
3526-
*/
3541+
ZEND_BEGIN_ARG_INFO(arginfo_offsetSet, 0)
3542+
ZEND_ARG_INFO(0, index)
3543+
ZEND_ARG_INFO(0, newval)
3544+
ZEND_END_ARG_INFO();
35273545
PHP_METHOD(CRubix, offsetSet)
35283546
{
3547+
CArray * target, * value;
3548+
MemoryPointer target_ptr, value_ptr;
3549+
int indexl;
3550+
zval *index, *val;
3551+
zval * obj = getThis();
3552+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &val) == FAILURE) {
3553+
return;
3554+
}
3555+
convert_to_long(index);
3556+
indexl = (int)zval_get_long(index);
3557+
ZVAL_TO_MEMORYPOINTER(val, &value_ptr, NULL);
3558+
ZVAL_TO_MEMORYPOINTER(obj, &target_ptr, NULL);
3559+
target = CArray_FromMemoryPointer(&target_ptr);
3560+
value = CArray_FromMemoryPointer(&value_ptr);
35293561

3530-
}
3562+
if ((int)indexl >= CArray_DIMS(target)[0]) {
3563+
throw_indexerror_exception("Invalid index");
3564+
return;
3565+
}
35313566

3567+
if (CArray_NDIM(target) == 1 && CArray_NDIM(value) == 0) {
3568+
if (CArray_TYPE(target) == TYPE_INTEGER_INT && CArray_TYPE(value) == TYPE_INTEGER_INT) {
3569+
IDATA(target)[indexl] = IDATA(value)[0];
3570+
}
3571+
if (CArray_TYPE(target) == TYPE_DOUBLE_INT && CArray_TYPE(value) == TYPE_DOUBLE_INT) {
3572+
DDATA(target)[indexl] = DDATA(value)[0];
3573+
}
3574+
if (CArray_TYPE(target) == TYPE_INTEGER_INT && CArray_TYPE(value) == TYPE_DOUBLE_INT) {
3575+
IDATA(target)[indexl] = (int)DDATA(value)[0];
3576+
}
3577+
if (CArray_TYPE(target) == TYPE_DOUBLE_INT && CArray_TYPE(value) == TYPE_INTEGER_INT) {
3578+
DDATA(target)[indexl] = (double)IDATA(value)[0];
3579+
}
3580+
FREE_FROM_MEMORYPOINTER(&target_ptr);
3581+
FREE_FROM_MEMORYPOINTER(&value_ptr);
3582+
return;
3583+
}
3584+
3585+
setArrayFromSequence(target, value, CArray_NDIM(value), ((int)indexl * CArray_STRIDES(target)[0]));
3586+
FREE_FROM_MEMORYPOINTER(&target_ptr);
3587+
FREE_FROM_MEMORYPOINTER(&value_ptr);
3588+
}
35323589
/**
35333590
* Rubix/Tensor/Matrix::offsetExists
35343591
*/

kernel/interfaces/rubix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ PHP_METHOD(CRubix, greaterEqualScalar); //OK PASSED
125125
PHP_METHOD(CRubix, lessScalar); //OK PASSED
126126
PHP_METHOD(CRubix, lessEqualScalar); //OK PASSED
127127
PHP_METHOD(CRubix, count);
128-
PHP_METHOD(CRubix, offsetSet);
128+
PHP_METHOD(CRubix, offsetSet); //OK PASSED
129129
PHP_METHOD(CRubix, offsetExists);
130130
PHP_METHOD(CRubix, offsetUnset);
131131
PHP_METHOD(CRubix, offsetGet);

phpsci.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,21 +341,37 @@ PHP_METHOD(CArray, offsetSet)
341341
{
342342
CArray * target, * value;
343343
MemoryPointer target_ptr, value_ptr;
344-
long indexl;
344+
int indexl;
345345
zval *index, *val;
346346
zval * obj = getThis();
347347
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &val) == FAILURE) {
348348
return;
349349
}
350350
convert_to_long(index);
351-
indexl = zval_get_double(index);
351+
indexl = (int)zval_get_long(index);
352352
ZVAL_TO_MEMORYPOINTER(val, &value_ptr, NULL);
353353
ZVAL_TO_MEMORYPOINTER(obj, &target_ptr, NULL);
354354
target = CArray_FromMemoryPointer(&target_ptr);
355355
value = CArray_FromMemoryPointer(&value_ptr);
356356

357357
if ((int)indexl >= CArray_DIMS(target)[0]) {
358-
throw_indexerror_exception("");
358+
throw_indexerror_exception("Invalid index");
359+
return;
360+
}
361+
362+
if (CArray_NDIM(target) == 1 && CArray_NDIM(value) == 0) {
363+
if (CArray_TYPE(target) == TYPE_INTEGER_INT && CArray_TYPE(value) == TYPE_INTEGER_INT) {
364+
IDATA(target)[indexl] = IDATA(value)[0];
365+
}
366+
if (CArray_TYPE(target) == TYPE_DOUBLE_INT && CArray_TYPE(value) == TYPE_DOUBLE_INT) {
367+
DDATA(target)[indexl] = DDATA(value)[0];
368+
}
369+
if (CArray_TYPE(target) == TYPE_INTEGER_INT && CArray_TYPE(value) == TYPE_DOUBLE_INT) {
370+
IDATA(target)[indexl] = (int)DDATA(value)[0];
371+
}
372+
if (CArray_TYPE(target) == TYPE_DOUBLE_INT && CArray_TYPE(value) == TYPE_INTEGER_INT) {
373+
DDATA(target)[indexl] = (double)IDATA(value)[0];
374+
}
359375
return;
360376
}
361377

@@ -2797,6 +2813,7 @@ static zend_function_entry crubix_class_methods[] =
27972813
PHP_ME(CRubix, sign, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
27982814
PHP_ME(CRubix, round, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
27992815
PHP_ME(CRubix, offsetGet, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
2816+
PHP_ME(CRubix, offsetSet, arginfo_offsetSet, ZEND_ACC_PUBLIC)
28002817
};
28012818

28022819
/**

0 commit comments

Comments
 (0)