Skip to content

Commit

Permalink
Add a Pointer type similar to the Mutable type
Browse files Browse the repository at this point in the history
The main difference is a pointer may be mutable or immutable.
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed May 2, 2017
1 parent f92a4a0 commit f618817
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -850,6 +850,9 @@ algorithm
i = listLength(el);
then DAE.ICONST(i);

case DAE.CALL(path=Absyn.IDENT("mmc_mk_some"),expLst={e})
then DAE.META_OPTION(SOME(e));

case DAE.CALL(path=Absyn.IDENT("sourceInfo"))
equation
print("sourceInfo() - simplify?\n");
Expand Down
85 changes: 85 additions & 0 deletions Compiler/Util/Pointer.mo
@@ -0,0 +1,85 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

encapsulated uniontype Pointer<T>
"Creating shared (sometimes mutable) objects.
This uniontype contains routines for creating and updating objects,
similar to array<> structures. Use this uniontype over the Mutable
package if you need to be able to create constants that are just
pointers to static, immutable data. Use the Mutable uniontype if you
do not need to create constants (that package has lower overhead
since it does no extra checks)."

impure function create
input T data;
output Pointer<T> ptr;
external "C" ptr=pointerCreate(data) annotation(Include="
static inline void* pointerCreate(void *data)
{
return mmc_mk_box1(0, data);
}
");
end create;

function createImmutable
input T data;
output Pointer<T> ptr;
external "builtin" ptr=mmc_mk_some(data);
end createImmutable;

impure function update
input Pointer<T> mutable;
input T data;
external "C" pointerUpdate(OpenModelica.threadData(), mutable, data) annotation(Include="
static inline void pointerUpdate(threadData_t *threadData, void *ptr, void *data)
{
if (valueConstructor(ptr)!=0) {
MMC_THROW_INTERNAL();
}
MMC_STRUCTDATA(ptr)[0] = data;
}
");
end update;

impure function access
input Pointer<T> mutable;
output T data;
external "C" data=pointerAccess(mutable) annotation(Include="
static inline void* pointerAccess(void *ptr)
{
return MMC_STRUCTDATA(ptr)[0];
}
");
end access;

annotation(__OpenModelica_Interface="util");
end Pointer;
1 change: 1 addition & 0 deletions Compiler/boot/LoadCompilerSources.mos
Expand Up @@ -218,6 +218,7 @@ if true then /* Suppress output */
"../Util/List.mo",
"../Util/ModelicaExternalC.mo",
"../Util/Mutable.mo",
"../Util/Pointer.mo",
"../Util/Print.mo",
"../Util/Settings.mo",
"../Util/StackOverflow.mo",
Expand Down
5 changes: 5 additions & 0 deletions SimulationRuntime/c/meta/meta_modelica.h
Expand Up @@ -124,6 +124,11 @@ static inline void *mmc_mk_some(void *x)
return mmc_mk_box1(1, x);
}

static inline void *mmc__mk__some(void *x)
{
return mmc_mk_some(x);
}

extern void *mmc_mk_box_arr(mmc_sint_t _slots, mmc_uint_t ctor, void** args);
static inline void *mmc_mk_box_no_assign(mmc_sint_t _slots, mmc_uint_t ctor, int is_atomic)
{
Expand Down

0 comments on commit f618817

Please sign in to comment.