<commit_before>/*
 * Copyright 2008 Google Inc.
 * Copyright 2007 Nintendo Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <map>

#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <wchar.h>

#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>

#include <es/apply.h>
#include <es/broker.h>
#include <es/dateTime.h>
#include <es/handle.h>
#include <es/interlocked.h>
#include <es/objectTable.h>
#include <es/ref.h>
#include <es/reflect.h>
#include <es/timeSpan.h>
#include <es/classFactory.h>
#include <es/clsid.h>
#include <es/base/IProcess.h>

#include <sys/mman.h>

#include "core.h"
#include "posix_system.h"
#include "posix_video.h"

// #define VERBOSE

using namespace es;
using namespace posix;

extern Reflect::Interface& getInterface(const Guid& iid);

namespace
{

static const int MAX_EXPORT = 100;
static const int MAX_IMPORT = 100;

// for RPC
__thread int epfd = -1;
__thread int rpctag;
__thread std::map<pid_t, int>* socketMap;

class RpcStack
{
    static const int RPC_STACK_SIZE = 1024 * 1024;
    static const int ALIGN = 1 << 3;
    static __thread u8 rpcStackBase[RPC_STACK_SIZE];
    static __thread u8* rpcStack;

    u8* base;

public:
    RpcStack()
    {
        base = rpcStack;
    }

    ~RpcStack()
    {
        // Restore rpcStack
        rpcStack = base;
    }

    static void init()
    {
        rpcStack = rpcStackBase;
    }

    static void* alloc(size_t size)
    {
        size += ALIGN - 1;
        size &= ~(ALIGN - 1);
        if (size <= getFreeSize())
        {
            return rpcStack += size;
        }
        return 0;
    }

    static void* free(size_t size)
    {
        size += ALIGN - 1;
        size &= ~(ALIGN - 1);
        if (rpcStackBase <= (rpcStack - size))
        {
            return rpcStack -= size;
        }
        return rpcStackBase;
    }

    static void* top()
    {
        return rpcStack;
    }

    static size_t getFreeSize()
    {
        return rpcStackBase + RPC_STACK_SIZE - rpcStack;
    }
};

struct ThreadCredential
{
    pid_t       pid;
    pthread_t   tid;
    u64         check;  // maybe optional
};<commit_msg>(RpcStack) : Add rpcStackBase and rpcStack definitions outside the class declaration.<commit_after>/*
 * Copyright 2008 Google Inc.
 * Copyright 2007 Nintendo Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <map>

#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <wchar.h>

#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>

#include <es/apply.h>
#include <es/broker.h>
#include <es/dateTime.h>
#include <es/handle.h>
#include <es/interlocked.h>
#include <es/objectTable.h>
#include <es/ref.h>
#include <es/reflect.h>
#include <es/timeSpan.h>
#include <es/classFactory.h>
#include <es/clsid.h>
#include <es/base/IProcess.h>

#include <sys/mman.h>

#include "core.h"
#include "posix_system.h"
#include "posix_video.h"

// #define VERBOSE

using namespace es;
using namespace posix;

extern Reflect::Interface& getInterface(const Guid& iid);

namespace
{

static const int MAX_EXPORT = 100;
static const int MAX_IMPORT = 100;

// for RPC
__thread int epfd = -1;
__thread int rpctag;
__thread std::map<pid_t, int>* socketMap;

class RpcStack
{
    static const int RPC_STACK_SIZE = 1024 * 1024;
    static const int ALIGN = 1 << 3;
    static __thread u8 rpcStackBase[RPC_STACK_SIZE];
    static __thread u8* rpcStack;

    u8* base;

public:
    RpcStack()
    {
        base = rpcStack;
    }

    ~RpcStack()
    {
        // Restore rpcStack
        rpcStack = base;
    }

    static void init()
    {
        rpcStack = rpcStackBase;
    }

    static void* alloc(size_t size)
    {
        size += ALIGN - 1;
        size &= ~(ALIGN - 1);
        if (size <= getFreeSize())
        {
            return rpcStack += size;
        }
        return 0;
    }

    static void* free(size_t size)
    {
        size += ALIGN - 1;
        size &= ~(ALIGN - 1);
        if (rpcStackBase <= (rpcStack - size))
        {
            return rpcStack -= size;
        }
        return rpcStackBase;
    }

    static void* top()
    {
        return rpcStack;
    }

    static size_t getFreeSize()
    {
        return rpcStackBase + RPC_STACK_SIZE - rpcStack;
    }
};

__thread u8 RpcStack::rpcStackBase[RPC_STACK_SIZE];
__thread u8* RpcStack::rpcStack;

struct ThreadCredential
{
    pid_t       pid;
    pthread_t   tid;
    u64         check;  // maybe optional
};
{'fraction_non_alphanumeric': 0.12075407608695653, 'fraction_numerical': 0.01358695652173913, 'mean_word_length': 2.7154574132492115, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4915439', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2030, 'n_words': 591}