summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/src/resource_control.h
blob: cadae0e2621e81151d13dcb05391844eb0b33cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*************************************************************************
 *                                                                       *
 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of EITHER:                                  *
 *   (1) 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. The text of the GNU Lesser      *
 *       General Public License is included with this library in the     *
 *       file LICENSE.TXT.                                               *
 *   (2) The BSD-style license that is included with this library in     *
 *       the file LICENSE-BSD.TXT.                                       *
 *                                                                       *
 * 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 files    *
 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
 *                                                                       *
 *************************************************************************/

/*
 * Resource accounting/preallocation class declarations
 * Copyright (c) 2017-2019 Oleh Derevenko, odar@eleks.com (change all "a" to "e")
 */

#ifndef _ODE__PRIVATE_RESOURCE_CONTRIOL_H_
#define _ODE__PRIVATE_RESOURCE_CONTRIOL_H_


#include "objects.h"
#include "threading_base.h"
#include "odeou.h"
#include "common.h"
#include "error.h"


using _OU_NAMESPACE::CSimpleFlags;


class dxResourceRequirementDescriptor:
    public dBase
{
public:
    explicit dxResourceRequirementDescriptor(dxThreadingBase *relatedThreading):
        dBase(),
        m_relatedThreading(relatedThreading),
        m_memorySizeRequirement(0),
        m_memoryAlignmentRequirement(0),
        m_simultaneousCallRequirement(0),
        m_featureRequirements()
    {
    }

    ~dxResourceRequirementDescriptor();

    enum
    {
        STOCK_CALLWAIT_REQUIRED = 0x00000001,
    };

    void mergeAnotherDescriptorIn(const dxResourceRequirementDescriptor &anotherDescriptor)
    {
        dIASSERT(getrelatedThreading() == anotherDescriptor.getrelatedThreading()); // m_simultaneousCallRequirement typically depends on threading used

        CSimpleFlags::value_type allOtherFeatureFlags = anotherDescriptor.queryAllFeatureFlags();
        mergeAnotherDescriptorIn(anotherDescriptor.m_memorySizeRequirement, anotherDescriptor.m_memoryAlignmentRequirement, anotherDescriptor.m_simultaneousCallRequirement, allOtherFeatureFlags);
    }

    void mergeAnotherDescriptorIn(sizeint memorySizeRequirement/*=0*/, unsigned memoryAlignmentRequirement,
        unsigned simultaneousCallRequirement/*=0*/, unsigned featureRequirement/*=0*/)
    {
        m_memorySizeRequirement = dMACRO_MAX(m_memorySizeRequirement, memorySizeRequirement);
        m_memoryAlignmentRequirement = dMACRO_MAX(m_memoryAlignmentRequirement, memoryAlignmentRequirement);
        m_simultaneousCallRequirement = dMACRO_MAX(m_simultaneousCallRequirement, simultaneousCallRequirement);
        mergeFeatureFlags(featureRequirement);
    }

public:
    dxThreadingBase *getrelatedThreading() const { return m_relatedThreading; }
    sizeint getMemorySizeRequirement() const { return m_memorySizeRequirement; }
    unsigned getMemoryAlignmentRequirement() const { return m_memoryAlignmentRequirement; }

    unsigned getSimultaneousCallRequirement() const { return m_simultaneousCallRequirement; }

    bool getIsStockCallWaitRequired() const { return getStockCallWaitRequiredFlag(); }

private:
    enum
    {
        FL_STOCK_CALLWAIT_REQUIRED  = STOCK_CALLWAIT_REQUIRED,
    };

    bool getStockCallWaitRequiredFlag() const { return m_featureRequirements.GetFlagsMaskValue(FL_STOCK_CALLWAIT_REQUIRED); }

    CSimpleFlags::value_type queryAllFeatureFlags() const { return m_featureRequirements.QueryFlagsAllValues(); }
    void mergeFeatureFlags(CSimpleFlags::value_type flagValues) { m_featureRequirements.SignalFlagsMaskValue(flagValues); }

private:
    dxThreadingBase     *m_relatedThreading;
    sizeint              m_memorySizeRequirement;
    unsigned            m_memoryAlignmentRequirement;
    unsigned            m_simultaneousCallRequirement;
    CSimpleFlags        m_featureRequirements;
};

static inline 
dxResourceRequirementDescriptor *decodeResourceRequirementsID(dResourceRequirementsID requirements)
{
    return (dxResourceRequirementDescriptor *)requirements;
}


class dxRequiredResourceContainer:
    public dBase
{
public:
    dxRequiredResourceContainer():
        dBase(),
        m_relatedThreading(NULL),
        m_stockCallWait(NULL),
        m_memoryAllocation()
    {
    }

    ~dxRequiredResourceContainer();

    bool allocateResources(const dxResourceRequirementDescriptor &requirementDescriptor);
    void freeResources();

public:
    dxThreadingBase *getThreadingInstance() const { return m_relatedThreading; }
    dCallWaitID getStockCallWait() const { return m_stockCallWait; }
    void *getMemoryBufferPointer() const { return m_memoryAllocation.getUserAreaPointer(); }
    sizeint getMemoryBufferSize() const { return m_memoryAllocation.getUserAreaSize(); }

private:
    dxThreadingBase     *m_relatedThreading;
    dCallWaitID         m_stockCallWait;
    dxAlignedAllocation m_memoryAllocation;
};

static inline 
dxRequiredResourceContainer *decodeResourceContainerID(dResourceContainerID resources)
{
    return (dxRequiredResourceContainer *)resources;
}


#endif // #ifndef _ODE__PRIVATE_RESOURCE_CONTRIOL_H_