<commit_before>/*
 * Copyright (c) 2008-2013 <NAME>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef MC_MISC_H
#define MC_MISC_H

#include <stdio.h>
#include <stddef.h>
#include <tchar.h>

#include <windows.h>
#include <commctrl.h>
#include <shlwapi.h>
#include <wingdi.h>
#include <windowsx.h>

#include "compat.h"
#include "debug.h"
#include "resource.h"
#include "version.h"


/***************************
 *** Miscelaneous Macros ***
 ***************************/

/* Mininum and maximum et al. */
#define MC_MIN(a,b)            ((a) < (b) ? (a) : (b))
#define MC_MAX(a,b)            ((a) > (b) ? (a) : (b))
#define MC_MIN3(a,b,c)         MC_MIN(MC_MIN((a), (b)), (c))
#define MC_MAX3(a,b,c)         MC_MAX(MC_MAX((a), (b)), (c))
#define MC_ABS(a)              ((a) >= 0 ? (a) : -(a))
#define MC_SIGN(a)             ((a) > 0 ? +1 : ((a) < 0 ? -1 : 0))

/* Specifier for variable-sized array (last member in structures) */
#if defined __STDC__ && __STDC_VERSION__ >= 199901L
    #define MC_VARARRAY_SIZE     /* empty */
#elif defined _MSC_VER && _MSC_VER >= 1500
    #define MC_VARARRAY_SIZE     /* empty */
#elif defined __GNUC__
    #define MC_VARARRAY_SIZE     0
#else
    #define MC_VARARRAY_SIZE     1
#endif

/* Get count of records in an array */
#define MC_ARRAY_SIZE(array)   (sizeof((array)) / sizeof((array)[0]))

/* Offset of struct member */
#if defined offsetof
    #define MC_OFFSETOF(type, member)   offsetof(type, member)
#elif defined MC_COMPILER_GCC  &&  MC_COMPILER_GCC >= 40000
    #define MC_OFFSETOF(type, member)   __builtin_offsetof(type, member)
#else
    #define MC_OFFSETOF(type, member)   ((size_t) &((type*)0)->member)
#endif

/* Pointer to container structure */
#define MC_CONTAINEROF(ptr, type, member)                                \
            ((type*)((BYTE*)(ptr) - MC_OFFSETOF(type, member)))


/* Macros telling the compiler that the condition is likely or unlikely to
 * be true. (If supported) it allows better optimization. Use only sparingly
 * in important loops where it really matters. Programmers are often bad in
 * this kind of prediction. */
#if defined MC_COMPILER_GCC  &&  MC_COMPILER_GCC >= 30000
    #define MC_LIKELY(condition)       __builtin_expect(!!(condition), !0)
    #define MC_UNLIKELY(condition)     __builtin_expect(!!(condition), 0)
#else
    #define MC_LIKELY(condition)       (condition)
    #define MC_UNLIKELY(condition)     (condition)
#endif

/* Macro for wrapping error conditions. */
#define MC_ERR(condition)              MC_UNLIKELY(condition)


/* Unicode resolution. */
#ifdef UNICODE
    #define MC_NAME_AW(name)           name##W
#else
    #define MC_NAME_AW(name)           name##A
#endif


/* SendMessage() wrapper to save some typing and casting. */
#define MC_MSG(win, msg, wp, lp)                                         \
            SendMessage((win), (msg), (WPARAM)(wp), (LPARAM)(lp))


/***************
 *** Globals ***
 ***************/

extern HINSTANCE mc_instance;


/* Checking OS version (compare with normal operators: ==, <, <= etc.) */

#define MC_WIN_VER(platform, major, minor)                               \
    (((platform) << 16) | ((major) << 8) | ((minor) << 0))

#define MC_WIN_95          MC_WIN_VER(1, 4, 0)
#define MC_WIN_98          MC_WIN_VER(1, 4, 10)
#define MC_WIN_ME          MC_WIN_VER(1, 4, 90)
#define MC_WIN_NT4         MC_WIN_VER(2, 4, 0)
#define MC_WIN_2000        MC_WIN_VER(2, 5, 0)
#define MC_WIN_XP          MC_WIN_VER(2, 5, 1)
#define MC_WIN_S2003       MC_WIN_VER(2, 5, 2)
#define MC_WIN_VISTA       MC_WIN_VER(2, 6, 0)
#define MC_WIN_S2008       MC_WIN_VER(2, 6, 0)
#define MC_WIN_7           MC_WIN_VER(2, 6, 1)
#define MC_WIN_S2008R2     MC_WIN_VER(2, 6, 1)
#define MC_WIN_8           MC_WIN_VER(2, 6, 2)

extern DWORD mc_win_version;


/* Checking version of COMCTRL32.DLL */

#define MC_DLL_VER(major, minor)                                         \
    (((major) << 16) | ((minor) << 0))

extern DWORD mc_comctl32_version;


/* Image list of glyphs used throughout mCtrl */

#define MC_BMP_GLYPH_W               9   /* glyph image size */
#define MC_BMP_GLYPH_H               9

#define MC_BMP_GLYPH_CLOSE           0   /* indexes of particular glyphs */
#define MC_BMP_GLYPH_MORE_OPTIONS    1
#define MC_BMP_GLYPH_CHEVRON_L       2
#define MC_BMP_GLYPH_CHEVRON_R       3
#define MC_BMP_GLYPH_EXPANDED        4
#define MC_BMP_GLYPH_COLLAPSED       5

extern HIMAGELIST mc_bmp_glyphs;


/*************************************
 *** Memory Manipulation Utilities ***
 *************************************/

/* memcpy/memmove can be slow because of 'call' instruction for very small
 * blocks of memory. Hence if we know the size is small already in compile
 * time, we may inline it instead.
 *
 * (gcc has a capability of inlining of these built-in.)
 */

#ifndef MC_COMPILER_GCC
    static inline void
    mc_inlined_memcpy(void* addr0, const void* addr1, size_t n)
    {
        BYTE* iter0 = (BYTE*) addr0;
        const BYTE* iter1 = (const BYTE*) addr1;

        while(n-- > 0)
            *iter0++ = *iter1++;
    }
#else
    #define mc_inlined_memcpy memcpy
#endif

#ifndef MC_COMPILER_GCC
mc_inlined_memmove(void* addr0, const void* addr1, size_t n)
    static inline void
    {
        BYTE* iter0 = (BYTE*) addr0;
        const BYTE* iter1 = (const BYTE*) addr1;<commit_msg>[src/misc.h] Fix mc_inlined_memmove() for non-GCC toolchains.<commit_after>/*
 * Copyright (c) 2008-2013 Martin Mitas
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef MC_MISC_H
#define MC_MISC_H

#include <stdio.h>
#include <stddef.h>
#include <tchar.h>

#include <windows.h>
#include <commctrl.h>
#include <shlwapi.h>
#include <wingdi.h>
#include <windowsx.h>

#include "compat.h"
#include "debug.h"
#include "resource.h"
#include "version.h"


/***************************
 *** Miscelaneous Macros ***
 ***************************/

/* Mininum and maximum et al. */
#define MC_MIN(a,b)            ((a) < (b) ? (a) : (b))
#define MC_MAX(a,b)            ((a) > (b) ? (a) : (b))
#define MC_MIN3(a,b,c)         MC_MIN(MC_MIN((a), (b)), (c))
#define MC_MAX3(a,b,c)         MC_MAX(MC_MAX((a), (b)), (c))
#define MC_ABS(a)              ((a) >= 0 ? (a) : -(a))
#define MC_SIGN(a)             ((a) > 0 ? +1 : ((a) < 0 ? -1 : 0))

/* Specifier for variable-sized array (last member in structures) */
#if defined __STDC__ && __STDC_VERSION__ >= 199901L
    #define MC_VARARRAY_SIZE     /* empty */
#elif defined _MSC_VER && _MSC_VER >= 1500
    #define MC_VARARRAY_SIZE     /* empty */
#elif defined __GNUC__
    #define MC_VARARRAY_SIZE     0
#else
    #define MC_VARARRAY_SIZE     1
#endif

/* Get count of records in an array */
#define MC_ARRAY_SIZE(array)   (sizeof((array)) / sizeof((array)[0]))

/* Offset of struct member */
#if defined offsetof
    #define MC_OFFSETOF(type, member)   offsetof(type, member)
#elif defined MC_COMPILER_GCC  &&  MC_COMPILER_GCC >= 40000
    #define MC_OFFSETOF(type, member)   __builtin_offsetof(type, member)
#else
    #define MC_OFFSETOF(type, member)   ((size_t) &((type*)0)->member)
#endif

/* Pointer to container structure */
#define MC_CONTAINEROF(ptr, type, member)                                \
            ((type*)((BYTE*)(ptr) - MC_OFFSETOF(type, member)))


/* Macros telling the compiler that the condition is likely or unlikely to
 * be true. (If supported) it allows better optimization. Use only sparingly
 * in important loops where it really matters. Programmers are often bad in
 * this kind of prediction. */
#if defined MC_COMPILER_GCC  &&  MC_COMPILER_GCC >= 30000
    #define MC_LIKELY(condition)       __builtin_expect(!!(condition), !0)
    #define MC_UNLIKELY(condition)     __builtin_expect(!!(condition), 0)
#else
    #define MC_LIKELY(condition)       (condition)
    #define MC_UNLIKELY(condition)     (condition)
#endif

/* Macro for wrapping error conditions. */
#define MC_ERR(condition)              MC_UNLIKELY(condition)


/* Unicode resolution. */
#ifdef UNICODE
    #define MC_NAME_AW(name)           name##W
#else
    #define MC_NAME_AW(name)           name##A
#endif


/* SendMessage() wrapper to save some typing and casting. */
#define MC_MSG(win, msg, wp, lp)                                         \
            SendMessage((win), (msg), (WPARAM)(wp), (LPARAM)(lp))


/***************
 *** Globals ***
 ***************/

extern HINSTANCE mc_instance;


/* Checking OS version (compare with normal operators: ==, <, <= etc.) */

#define MC_WIN_VER(platform, major, minor)                               \
    (((platform) << 16) | ((major) << 8) | ((minor) << 0))

#define MC_WIN_95          MC_WIN_VER(1, 4, 0)
#define MC_WIN_98          MC_WIN_VER(1, 4, 10)
#define MC_WIN_ME          MC_WIN_VER(1, 4, 90)
#define MC_WIN_NT4         MC_WIN_VER(2, 4, 0)
#define MC_WIN_2000        MC_WIN_VER(2, 5, 0)
#define MC_WIN_XP          MC_WIN_VER(2, 5, 1)
#define MC_WIN_S2003       MC_WIN_VER(2, 5, 2)
#define MC_WIN_VISTA       MC_WIN_VER(2, 6, 0)
#define MC_WIN_S2008       MC_WIN_VER(2, 6, 0)
#define MC_WIN_7           MC_WIN_VER(2, 6, 1)
#define MC_WIN_S2008R2     MC_WIN_VER(2, 6, 1)
#define MC_WIN_8           MC_WIN_VER(2, 6, 2)

extern DWORD mc_win_version;


/* Checking version of COMCTRL32.DLL */

#define MC_DLL_VER(major, minor)                                         \
    (((major) << 16) | ((minor) << 0))

extern DWORD mc_comctl32_version;


/* Image list of glyphs used throughout mCtrl */

#define MC_BMP_GLYPH_W               9   /* glyph image size */
#define MC_BMP_GLYPH_H               9

#define MC_BMP_GLYPH_CLOSE           0   /* indexes of particular glyphs */
#define MC_BMP_GLYPH_MORE_OPTIONS    1
#define MC_BMP_GLYPH_CHEVRON_L       2
#define MC_BMP_GLYPH_CHEVRON_R       3
#define MC_BMP_GLYPH_EXPANDED        4
#define MC_BMP_GLYPH_COLLAPSED       5

extern HIMAGELIST mc_bmp_glyphs;


/*************************************
 *** Memory Manipulation Utilities ***
 *************************************/

/* memcpy/memmove can be slow because of 'call' instruction for very small
 * blocks of memory. Hence if we know the size is small already in compile
 * time, we may inline it instead.
 *
 * (gcc has a capability of inlining of these built-in.)
 */

#ifndef MC_COMPILER_GCC
    static inline void
    mc_inlined_memcpy(void* addr0, const void* addr1, size_t n)
    {
        BYTE* iter0 = (BYTE*) addr0;
        const BYTE* iter1 = (const BYTE*) addr1;

        while(n-- > 0)
            *iter0++ = *iter1++;
    }
#else
    #define mc_inlined_memcpy memcpy
#endif

#ifndef MC_COMPILER_GCC
    static inline void
    mc_inlined_memmove(void* addr0, const void* addr1, size_t n)
    {
        BYTE* iter0 = (BYTE*) addr0;
        const BYTE* iter1 = (const BYTE*) addr1;
{'fraction_non_alphanumeric': 0.13991090579112359, 'fraction_numerical': 0.024913380630259035, 'mean_word_length': 3.0102547138604034, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1614482', 'n_tokens_mistral': 4378, 'n_tokens_neox': 4067, 'n_words': 1229}