initial data structure

This commit is contained in:
Jacques Lucke 2022-01-03 15:29:23 +01:00
parent 2e00d90938
commit e0ac932156
5 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,45 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
/** \file
* \ingroup bli
*/
#include "BLI_compiler_attrs.h"
#include "DNA_copy_on_write.h"
#ifdef __cplusplus
extern "C" {
#endif
bCopyOnWrite *BLI_cow_new(int user_count);
void BLI_cow_free(const bCopyOnWrite *cow);
void BLI_cow_init(const bCopyOnWrite *cow, int user_count);
bool BLI_cow_is_shared(const bCopyOnWrite *cow);
bool BLI_cow_is_mutable(const bCopyOnWrite *cow);
bool BLI_cow_is_zero(const bCopyOnWrite *cow);
void BLI_cow_user_add(const bCopyOnWrite *cow);
bool BLI_cow_user_remove(const bCopyOnWrite *cow) ATTR_WARN_UNUSED_RESULT;
#ifdef __cplusplus
}
#endif

View File

@ -68,6 +68,7 @@ set(SRC
intern/boxpack_2d.c
intern/buffer.c
intern/convexhull_2d.c
intern/copy_on_write.cc
intern/delaunay_2d.cc
intern/dot_export.cc
intern/dynlib.c
@ -187,6 +188,7 @@ set(SRC
BLI_compiler_typecheck.h
BLI_console.h
BLI_convexhull_2d.h
BLI_copy_on_write.h
BLI_delaunay_2d.h
BLI_dial_2d.h
BLI_disjoint_set.hh

View File

@ -0,0 +1,78 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup bli
*/
#include "atomic_ops.h"
#include "BLI_assert.h"
#include "BLI_copy_on_write.h"
#include "MEM_guardedalloc.h"
static int &get_counter(const bCopyOnWrite *cow)
{
BLI_assert(cow != nullptr);
return *const_cast<int *>(&cow->user_count);
}
bCopyOnWrite *BLI_cow_new(int user_count)
{
bCopyOnWrite *cow = MEM_cnew<bCopyOnWrite>(__func__);
cow->user_count = user_count;
return cow;
}
void BLI_cow_free(const bCopyOnWrite *cow)
{
BLI_assert(cow->user_count == 0);
MEM_freeN(const_cast<bCopyOnWrite *>(cow));
}
void BLI_cow_init(const bCopyOnWrite *cow, int user_count)
{
get_counter(cow) = user_count;
}
bool BLI_cow_is_mutable(const bCopyOnWrite *cow)
{
return !BLI_cow_is_shared(cow);
}
bool BLI_cow_is_shared(const bCopyOnWrite *cow)
{
return cow->user_count >= 2;
}
bool BLI_cow_is_zero(const bCopyOnWrite *cow)
{
return cow->user_count == 0;
}
void BLI_cow_user_add(const bCopyOnWrite *cow)
{
atomic_fetch_and_add_int32(&get_counter(cow), 1);
}
bool BLI_cow_user_remove(const bCopyOnWrite *cow)
{
const int new_user_count = atomic_sub_and_fetch_int32(&get_counter(cow), 1);
BLI_assert(new_user_count >= 0);
const bool has_no_user_anymore = new_user_count == 0;
return has_no_user_anymore;
}

View File

@ -0,0 +1,25 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup DNA
*/
#pragma once
typedef struct bCopyOnWrite {
int user_count;
} bCopyOnWrite;

View File

@ -141,6 +141,7 @@ static const char *includefiles[] = {
"DNA_pointcache_types.h",
"DNA_uuid_types.h",
"DNA_asset_types.h",
"DNA_copy_on_write.h",
/* see comment above before editing! */
@ -1624,6 +1625,7 @@ int main(int argc, char **argv)
#include "DNA_collection_types.h"
#include "DNA_color_types.h"
#include "DNA_constraint_types.h"
#include "DNA_copy_on_write.h"
#include "DNA_curve_types.h"
#include "DNA_curveprofile_types.h"
#include "DNA_customdata_types.h"