##// END OF EJS Templates
rust-cpython: mark all PyLeaked methods as unsafe...
rust-cpython: mark all PyLeaked methods as unsafe Unfortunately, these methods can be abused to obtain the inner 'static reference. The simplest (pseudo-code) example is: let leaked: PyLeaked<&'static _> = shared.leak_immutable(); let static_ref: &'static _ = &*leaked.try_borrow(py)?; // PyLeakedRef::deref() tries to bound the lifetime to itself, but // the underlying data is a &'static reference, so the returned // reference can be &'static. This problem can be easily fixed by coercing the lifetime, but there are many other ways to achieve that, and there wouldn't be a generic solution: let leaked: PyLeaked<&'static [_]> = shared.leak_immutable(); let leaked_iter: PyLeaked<slice::Iter<'static, _>> = unsafe { leaked.map(|v| v.iter()) }; let static_slice: &'static [_] = leaked_iter.try_borrow(py)?.as_slice(); So basically I failed to design the safe borrowing interface. Maybe we'll instead have to add much more restricted interface on top of the unsafe PyLeaked methods? For instance, Iterator::next() could be implemented if its Item type is not &'a (where 'a may be cheated.) Anyway, this seems not an easy issue, so it's probably better to leave the current interface as unsafe, and get broader comments while upstreaming this feature.

File last commit:

r42237:675775c3 default
r44689:e960c30d default
Show More
zstd_common.c
83 lines | 2.6 KiB | text/x-c | CLexer
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 /*
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
* All rights reserved.
*
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 * This source code is licensed under both the BSD-style license (found in the
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
* in the COPYING file in the root directory of this source tree).
* You may select, at your option, one of the above-listed licenses.
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 */
/*-*************************************
* Dependencies
***************************************/
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 #include <stdlib.h> /* malloc, calloc, free */
#include <string.h> /* memset */
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 #include "error_private.h"
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 #include "zstd_internal.h"
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434
/*-****************************************
* Version
******************************************/
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434
/*-****************************************
* ZSTD Error Management
******************************************/
Gregory Szorc
zstandard: vendor python-zstandard 0.11...
r42237 #undef ZSTD_isError /* defined within zstd_internal.h */
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 /*! ZSTD_isError() :
Gregory Szorc
zstandard: vendor python-zstandard 0.11...
r42237 * tells if a return value is an error code
* symbol is required for external callers */
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
/*! ZSTD_getErrorName() :
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 * provides error code string from function result (useful for debugging) */
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
/*! ZSTD_getError() :
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 * convert a `size_t` function result into a proper ZSTD_errorCode enum */
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
/*! ZSTD_getErrorString() :
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 * provides error code string from enum */
const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434
/*=**************************************************************
* Custom allocator
****************************************************************/
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 {
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 if (customMem.customAlloc)
return customMem.customAlloc(customMem.opaque, size);
return malloc(size);
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 }
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 {
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 if (customMem.customAlloc) {
/* calloc implemented as malloc+memset;
* not as efficient as calloc, but next best guess for custom malloc */
void* const ptr = customMem.customAlloc(customMem.opaque, size);
memset(ptr, 0, size);
return ptr;
}
return calloc(1, size);
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 }
void ZSTD_free(void* ptr, ZSTD_customMem customMem)
{
Gregory Szorc
zstandard: vendor python-zstandard 0.9.0...
r37513 if (ptr!=NULL) {
if (customMem.customFree)
customMem.customFree(customMem.opaque, ptr);
else
free(ptr);
}
Gregory Szorc
zstd: vendor zstd 1.1.1...
r30434 }