ReactOS 0.4.16-dev-715-ga1a169f
align.cpp File Reference
#include <corecrt_internal.h>
#include <malloc.h>
Include dependency graph for align.cpp:

Go to the source code of this file.

Macros

#define IS_2_POW_N(X)   ((X) != 0 && ((X) & ((X) - 1)) == 0)
 
#define PTR_SZ   sizeof(void *)
 

Functions

static void _aligned_free_base (_Pre_maybenull_ _Post_invalid_ void *block)
 
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_ (size) static void *_aligned_malloc_base(_In_ size_t size
 
static _Check_return_ size_t _aligned_msize_base (_Pre_notnull_ void *block, _In_ size_t alignment, _In_ size_t offset)
 
 _Success_ (return !=0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(size) static void *_aligned_offset_realloc_base(_Pre_maybenull_ _Post_invalid_ void *block
 
static __forceinline void *__cdecl _aligned_malloc_base (size_t const size, size_t const alignment)
 
static __forceinline void *__cdecl _aligned_offset_malloc_base (size_t size, size_t align, size_t offset)
 
static __forceinline void *__cdecl _aligned_realloc_base (void *const block, size_t const size, size_t const alignment)
 
static __forceinline void *__cdecl _aligned_recalloc_base (void *const block, size_t const count, size_t const size, size_t const alignment)
 
static __forceinline void *__cdecl _aligned_offset_realloc_base (void *block, size_t size, size_t align, size_t offset)
 
static __forceinline size_t __cdecl _aligned_msize_base (void *block, size_t align, size_t offset)
 
static __forceinline void *__cdecl _aligned_offset_recalloc_base (void *block, size_t count, size_t size, size_t align, size_t offset)
 
static __forceinline void __cdecl _aligned_free_base (void *const block)
 
 __declspec (noinline) void __cdecl _aligned_free(void *const block)
 

Variables

_Check_return_ _Ret_maybenull_ _In_ size_t alignment
 
_Check_return_ _Ret_maybenull_ _In_ size_t _In_ size_t offset
 
_In_ size_t size
 
_In_ size_t count
 

Macro Definition Documentation

◆ IS_2_POW_N

#define IS_2_POW_N (   X)    ((X) != 0 && ((X) & ((X) - 1)) == 0)

Definition at line 23 of file align.cpp.

◆ PTR_SZ

#define PTR_SZ   sizeof(void *)

Definition at line 24 of file align.cpp.

Function Documentation

◆ __declspec()

__declspec ( noinline  ) const

Definition at line 538 of file align.cpp.

539{
540 #ifdef _DEBUG
542 #else
544 #endif
545}
static void _aligned_free_base(_Pre_maybenull_ _Post_invalid_ void *block)
#define _aligned_free_dbg(p)
Definition: crtdbg.h:215
static unsigned int block
Definition: xmlmemory.c:101

◆ _aligned_free_base() [1/2]

static void _aligned_free_base ( _Pre_maybenull_ _Post_invalid_ void block)
static

◆ _aligned_free_base() [2/2]

static __forceinline void __cdecl _aligned_free_base ( void *const  block)
static

Definition at line 516 of file align.cpp.

517{
519
520 if (block == nullptr)
521 return;
522
524
525 /* ptr points to the pointer to starting of the memory block */
526 ptr = (ptr & ~(PTR_SZ -1)) - PTR_SZ;
527
528 /* ptr is the pointer to the start of memory block*/
529 ptr = *((uintptr_t*)ptr);
530 free((void *)ptr);
531}
#define PTR_SZ
Definition: align.cpp:24
#define free
Definition: debug_ros.c:5
unsigned int uintptr_t
Definition: intrin.h:47
static PVOID ptr
Definition: dispmode.c:27

◆ _aligned_malloc_base()

static __forceinline void *__cdecl _aligned_malloc_base ( size_t const  size,
size_t const  alignment 
)
static

Definition at line 117 of file align.cpp.

121{
123}
_Check_return_ _Ret_maybenull_ _In_ size_t alignment
Definition: align.cpp:48
static __forceinline void *__cdecl _aligned_offset_malloc_base(size_t size, size_t align, size_t offset)
Definition: align.cpp:147
GLsizeiptr size
Definition: glext.h:5919

◆ _aligned_msize_base() [1/2]

static _Check_return_ size_t _aligned_msize_base ( _Pre_notnull_ void block,
_In_ size_t  alignment,
_In_ size_t  offset 
)
static

◆ _aligned_msize_base() [2/2]

static __forceinline size_t __cdecl _aligned_msize_base ( void block,
size_t  align,
size_t  offset 
)
static

Definition at line 396 of file align.cpp.

401{
402 size_t header_size = 0; /* Size of the header block */
403 size_t footer_size = 0; /* Size of the footer block */
404 size_t total_size = 0; /* total size of the allocated block */
405 size_t user_size = 0; /* size of the user block*/
406 uintptr_t gap = 0; /* keep the alignment of the data block */
407 /* after the sizeof(void*) aligned pointer */
408 /* to the beginning of the allocated block */
409 uintptr_t ptr = 0; /* computes the beginning of the allocated block */
410
411 _VALIDATE_RETURN(block != nullptr, EINVAL, static_cast<size_t>(-1));
412
413 /* HEADER SIZE + FOOTER SIZE = GAP + ALIGN + SIZE OF A POINTER*/
414 /* HEADER SIZE + USER SIZE + FOOTER SIZE = TOTAL SIZE */
415
416 ptr = (uintptr_t)block; /* ptr points to the start of the aligned memory block */
417 ptr = (ptr & ~(PTR_SZ - 1)) - PTR_SZ; /* ptr is one position behind memblock */
418 /* the value in ptr is the start of the real allocated block */
419 ptr = *((uintptr_t *)ptr); /* after dereference ptr points to the beginning of the allocated block */
420
421 total_size = _msize_base((void*)ptr);
422 header_size = (uintptr_t) block - ptr;
423 gap = (0 - offset) & (PTR_SZ - 1);
424 /* Alignment cannot be less than sizeof(void*) */
425 align = (align > PTR_SZ ? align : PTR_SZ) -1;
426 footer_size = gap + align + PTR_SZ - header_size;
427 user_size = total_size - header_size - footer_size;
428
429 return user_size;
430}
#define EINVAL
Definition: acclib.h:90
_Check_return_ _Ret_maybenull_ _In_ size_t _In_ size_t offset
Definition: align.cpp:62
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
_Check_return_ _ACRTIMP size_t __cdecl _msize_base(_Pre_notnull_ void *_Block) _CRT_NOEXCEPT
int align(int length, int align)
Definition: dsound8.c:36

◆ _aligned_offset_malloc_base()

static __forceinline void *__cdecl _aligned_offset_malloc_base ( size_t  size,
size_t  align,
size_t  offset 
)
static

Definition at line 147 of file align.cpp.

152{
153 uintptr_t ptr, retptr, gap;
154 size_t nonuser_size,block_size;
155
156 /* validation section */
158 _VALIDATE_RETURN(offset == 0 || offset < size, EINVAL, nullptr);
159
160 align = (align > PTR_SZ ? align : PTR_SZ) -1;
161
162 /* gap = number of bytes needed to round up offset to align with PTR_SZ*/
163 gap = (0 - offset)&(PTR_SZ -1);
164
165 nonuser_size = PTR_SZ +gap +align;
166 block_size = nonuser_size + size;
168
169 if ( (ptr =(uintptr_t)malloc(block_size)) == (uintptr_t)nullptr)
170 return nullptr;
171
172 retptr =((ptr +nonuser_size+offset)&~align)- offset;
173 ((uintptr_t *)(retptr - gap))[-1] = ptr;
174
175 return (void *)retptr;
176}
#define ENOMEM
Definition: acclib.h:84
_In_ size_t size
Definition: align.cpp:67
#define IS_2_POW_N(X)
Definition: align.cpp:23
return
Definition: dirsup.c:529
#define malloc
Definition: debug_ros.c:4
GLintptr offset
Definition: glext.h:5920
#define _VALIDATE_RETURN_NOEXC(expr, errorcode, retexpr)
static DWORD block_size(DWORD block)
Definition: jsutils.c:66
if(dx< 0)
Definition: linetemp.h:194

Referenced by _aligned_malloc_base(), and _aligned_offset_realloc_base().

◆ _aligned_offset_realloc_base()

static __forceinline void *__cdecl _aligned_offset_realloc_base ( void block,
size_t  size,
size_t  align,
size_t  offset 
)
static

Definition at line 283 of file align.cpp.

289{
290 uintptr_t ptr, retptr, gap, stptr, diff;
291 uintptr_t movsz, reqsz;
292 int bFree = 0;
293
294 /* special cases */
295 if (block == nullptr)
296 {
298 }
299 if (size == 0)
300 {
302 return nullptr;
303 }
304
305 /* validation section */
307 _VALIDATE_RETURN(offset == 0 || offset < size, EINVAL, nullptr);
308
309 stptr = (uintptr_t)block;
310
311 /* ptr points to the pointer to starting of the memory block */
312 stptr = (stptr & ~(PTR_SZ -1)) - PTR_SZ;
313
314 /* ptr is the pointer to the start of memory block*/
315 stptr = *((uintptr_t *)stptr);
316
317 align = (align > PTR_SZ ? align : PTR_SZ) -1;
318 /* gap = number of bytes needed to round up offset to align with PTR_SZ*/
319 gap = (0 -offset)&(PTR_SZ -1);
320
321 diff = (uintptr_t)block - stptr;
322 /* Mov size is min of the size of data available and sizw requested.
323 */
324 #pragma warning(push)
325 #pragma warning(disable: 22018) // Silence prefast about overflow/underflow
326 movsz = _msize_base((void *)stptr) - ((uintptr_t)block - stptr);
327 #pragma warning(pop)
328
329 movsz = movsz > size ? size : movsz;
330 reqsz = PTR_SZ + gap + align + size;
331
332 _VALIDATE_RETURN_NOEXC(size <= reqsz, ENOMEM, nullptr);
333
334 /* First check if we can expand(reducing or expanding using expand) data
335 * safely, ie no data is lost. eg, reducing alignment and keeping size
336 * same might result in loss of data at the tail of data block while
337 * expanding.
338 *
339 * If no, use malloc to allocate the new data and move data.
340 *
341 * If yes, expand and then check if we need to move the data.
342 */
343 if ((stptr +align +PTR_SZ +gap)<(uintptr_t)block)
344 {
345 if ((ptr = (uintptr_t)malloc(reqsz)) == (uintptr_t) nullptr)
346 return nullptr;
347 bFree = 1;
348 }
349 else
350 {
351 /* we need to save errno, which can be modified by _expand */
352 errno_t save_errno = errno;
353 if ((ptr = (uintptr_t)_expand((void *)stptr, reqsz)) == (uintptr_t)nullptr)
354 {
355 errno = save_errno;
356 if ((ptr = (uintptr_t)malloc(reqsz)) == (uintptr_t) nullptr)
357 return nullptr;
358 bFree = 1;
359 }
360 else
361 stptr = ptr;
362 }
363
364
365 if ( ptr == ((uintptr_t)block - diff)
366 && !( ((size_t)block + gap +offset) & ~(align) ))
367 {
368 return block;
369 }
370
371 retptr =((ptr +PTR_SZ +gap +align +offset)&~align)- offset;
372 memmove((void *)retptr, (void *)(stptr + diff), movsz);
373 if ( bFree)
374 free ((void *)stptr);
375
376 ((uintptr_t *)(retptr - gap))[-1] = ptr;
377 return (void *)retptr;
378}
void * _expand(void *_ptr, size_t _size)
Definition: malloc.c:100
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
#define errno
Definition: errno.h:18
int errno_t
Definition: corecrt.h:615

Referenced by _aligned_offset_recalloc_base(), and _aligned_realloc_base().

◆ _aligned_offset_recalloc_base()

static __forceinline void *__cdecl _aligned_offset_recalloc_base ( void block,
size_t  count,
size_t  size,
size_t  align,
size_t  offset 
)
static

Definition at line 461 of file align.cpp.

468{
469 size_t user_size = 0; /* wanted size, passed to aligned realoc */
470 size_t start_fill = 0; /* location where aligned recalloc starts to fill with 0 */
471 /* filling must start from the end of the previous user block */
472 void * retptr = nullptr; /* result of aligned recalloc*/
473
474 /* ensure that (size * num) does not overflow */
475 if (count > 0)
476 {
478 }
479
480 user_size = size * count;
481
482 if (block != nullptr)
483 {
484 start_fill = _aligned_msize_base(block, align, offset);
485 }
486
487 retptr = _aligned_offset_realloc_base(block, user_size, align, offset);
488
489 if (retptr != nullptr)
490 {
491 if (start_fill < user_size)
492 {
493 memset ((char*)retptr + start_fill, 0, user_size - start_fill);
494 }
495 }
496 return retptr;
497}
_In_ size_t count
Definition: align.cpp:75
static __forceinline void *__cdecl _aligned_offset_realloc_base(void *block, size_t size, size_t align, size_t offset)
Definition: align.cpp:283
static _Check_return_ size_t _aligned_msize_base(_Pre_notnull_ void *block, _In_ size_t alignment, _In_ size_t offset)
#define _HEAP_MAXREQ
Definition: malloc.h:24
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define memset(x, y, z)
Definition: compat.h:39

Referenced by _aligned_recalloc_base().

◆ _aligned_realloc_base()

static __forceinline void *__cdecl _aligned_realloc_base ( void *const  block,
size_t const  size,
size_t const  alignment 
)
static

Definition at line 206 of file align.cpp.

211{
213}

◆ _aligned_recalloc_base()

static __forceinline void *__cdecl _aligned_recalloc_base ( void *const  block,
size_t const  count,
size_t const  size,
size_t const  alignment 
)
static

Definition at line 244 of file align.cpp.

250{
252}
static __forceinline void *__cdecl _aligned_offset_recalloc_base(void *block, size_t count, size_t size, size_t align, size_t offset)
Definition: align.cpp:461

◆ _Post_writable_byte_size_()

_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_ ( size  )

◆ _Success_()

_Success_ ( return = 0)

Definition at line 1028 of file fsctrl.c.

3189{
3191
3192 PVPB Vpb = Vcb->Vpb;
3193 PVPB OldVpb;
3194
3195 BOOLEAN Remount = FALSE;
3196
3197 PAGED_CODE();
3198
3199 UNREFERENCED_PARAMETER( IrpContext );
3200
3201 //
3202 // Check whether we are looking for a device only Mvcb.
3203 //
3204
3205 for (Link = CdData.VcbQueue.Flink;
3206 Link != &CdData.VcbQueue;
3207 Link = Link->Flink) {
3208
3209 *OldVcb = CONTAINING_RECORD( Link, VCB, VcbLinks );
3210
3211 //
3212 // Skip ourselves.
3213 //
3214
3215 if (Vcb == *OldVcb) { continue; }
3216
3217 //
3218 // Look at the Vpb and state of the previous Vcb.
3219 //
3220
3221 OldVpb = (*OldVcb)->Vpb;
3222
3223 if ((OldVpb != Vpb) &&
3224 (OldVpb->RealDevice == Vpb->RealDevice) &&
3225 ((*OldVcb)->VcbCondition == VcbNotMounted)) {
3226
3227 //
3228 // If the current disk is a raw disk then it can match a previous music or
3229 // raw disk.
3230 //
3231
3232 if (FlagOn( Vcb->VcbState, VCB_STATE_AUDIO_DISK)) {
3233
3234 if (FlagOn( (*OldVcb)->VcbState, VCB_STATE_AUDIO_DISK )) {
3235
3236 //
3237 // If we have both TOC then fail the remount if the lengths
3238 // are different or they don't match.
3239 //
3240
3241 if ((Vcb->TocLength != (*OldVcb)->TocLength) ||
3242 ((Vcb->TocLength != 0) &&
3243 !RtlEqualMemory( Vcb->CdromToc,
3244 (*OldVcb)->CdromToc,
3245 Vcb->TocLength ))) {
3246
3247 continue;
3248 }
3249
3250 Remount = TRUE;
3251 break;
3252 }
3253
3254 //
3255 // The current disk is not a raw disk. Go ahead and compare
3256 // serial numbers, volume label and TOC.
3257 //
3258
3259 }
3260 else if ((OldVpb->SerialNumber == Vpb->SerialNumber) &&
3261 (Vcb->TocLength == (*OldVcb)->TocLength) &&
3262 ((Vcb->TocLength == 0) || RtlEqualMemory( Vcb->CdromToc,
3263 (*OldVcb)->CdromToc,
3264 Vcb->TocLength )) &&
3265 (Vpb->VolumeLabelLength == OldVpb->VolumeLabelLength) &&
3266 (RtlEqualMemory( OldVpb->VolumeLabel,
3267 Vpb->VolumeLabel,
3268 Vpb->VolumeLabelLength ))) {
3269 //
3270 // Remember the old Vcb. Then set the return value to
3271 // TRUE and break.
3272 //
3273
3274 Remount = TRUE;
3275 break;
3276 }
3277 }
3278 }
3279
3280 return Remount;
3281}
#define PAGED_CODE()
unsigned char BOOLEAN
CD_DATA CdData
Definition: cddata.c:42
#define VCB_STATE_AUDIO_DISK
Definition: cdstruc.h:712
@ VcbNotMounted
Definition: cdstruc.h:490
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define FlagOn(_F, _SF)
Definition: ext2fs.h:179
IN OUT PVCB IN PDEVICE_OBJECT IN PVPB Vpb
Definition: fatprocs.h:1676
#define RtlEqualMemory(dst, src, len)
Definition: kdvm.h:18
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:325
#define Vcb
Definition: cdprocs.h:1415
LIST_ENTRY VcbQueue
Definition: cdstruc.h:334
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
Definition: cdstruc.h:498
Definition: iotypes.h:189
WCHAR VolumeLabel[MAXIMUM_VOLUME_LABEL_LENGTH/sizeof(WCHAR)]
Definition: iotypes.h:198
USHORT VolumeLabelLength
Definition: iotypes.h:193
ULONG SerialNumber
Definition: iotypes.h:196
struct _DEVICE_OBJECT * RealDevice
Definition: iotypes.h:195
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
static int Link(const char **args)
Definition: vfdcmd.c:2414

Variable Documentation

◆ alignment

Initial value:
{
static __forceinline void *__cdecl _aligned_malloc_base(size_t const size, size_t const alignment)
Definition: align.cpp:117

Definition at line 47 of file align.cpp.

Referenced by _aligned_malloc(), _aligned_malloc_base(), _aligned_malloc_dbg(), _aligned_msize_dbg(), _aligned_offset_malloc(), _aligned_offset_malloc_dbg(), _aligned_offset_realloc(), _aligned_offset_realloc_dbg(), _aligned_offset_recalloc_dbg(), _aligned_realloc(), _aligned_realloc_base(), _aligned_realloc_dbg(), _aligned_recalloc_base(), _aligned_recalloc_dbg(), add_typedef_typeinfo(), add_var_desc(), align_the_pointer(), array_buffer_size(), array_memory_size(), array_read_variance_and_unmarshall(), array_write_variance_and_marshall(), DesktopSettingsDlg::Command(), FxDmaEnabler::ConfigureBusMasterAdapters(), FT_Bitmap_Convert(), get_function_buffer_size(), get_parameter_fc(), get_required_buffer_size(), get_required_buffer_size_type(), StringFormat::GetAlignment(), StringFormat::GetLineAlignment(), gl_pixel_addr_in_image(), ICreateTypeInfo2_fnSetAlignment(), NdrVaryingArrayBufferSize(), NdrVaryingArrayMarshall(), NdrVaryingArrayMemorySize(), NdrVaryingArrayUnmarshall(), print_phase_basetype(), psh_blues_snap_stem(), ScsiFlopProcessError(), test_aligned_malloc(), test_aligned_offset_malloc(), test_aligned_offset_realloc(), test_aligned_realloc(), test_CreateTypeLib(), testTDA(), VfdOpenImage(), vio_modern_map_capability(), vio_modern_map_simple_capability(), wined3d_format_calculate_pitch(), wined3d_format_calculate_size(), and write_remoting_arg().

◆ count

Definition at line 75 of file align.cpp.

Referenced by _aligned_offset_recalloc_base().

◆ offset

◆ size