Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
openfpm_devices
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sbalzarini Lab
Software
Parallel Computing
OpenFPM
openfpm_devices
Commits
f2ca1d3b
Commit
f2ca1d3b
authored
9 years ago
by
Pietro Incardona
Browse files
Options
Downloads
Patches
Plain Diff
Add PtrMemory.hpp
parent
fea22a84
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/memory/PtrMemory.hpp
+127
-0
127 additions, 0 deletions
src/memory/PtrMemory.hpp
with
127 additions
and
0 deletions
src/memory/PtrMemory.hpp
0 → 100644
+
127
−
0
View file @
f2ca1d3b
/*
* PtrMemory.hpp
*
* Created on: Apr 15, 2015
* Author: Pietro Incardona
*/
#ifndef PTRMEMORY_HPP_
#define PTRMEMORY_HPP_
/**
* \brief This class give memory from a preallocated memory, memory destruction is not performed
*
* Useful to shape pieces of memory
*
* Usage:
*
* void * ptr = new int[1000]
*
* PtrMemory m = new PtrMemory(ptr,1000);
*
* m.allocate();
* int * ptr = m.getPointer();
* *ptr[999] = 1000;
* ....
*
* delete[] ptr;
*
*/
#include
"config.h"
#include
"memory.hpp"
#include
<cstddef>
#include
<cstdint>
#include
<iostream>
#ifdef MEMLEAK_CHECK
#include
"Memleak_check.hpp"
#endif
class
PtrMemory
:
public
memory
{
//! Size of the pointed memory
size_t
spm
;
//! Pointed memory
void
*
dm
;
//! Size of the memory
size_t
sz
;
//! Reference counter
long
int
ref_cnt
;
//! copy from same Heap to Heap
bool
copyDeviceToDevice
(
PtrMemory
&
m
);
//! copy from Pointer to Heap
bool
copyFromPointer
(
void
*
ptr
,
size_t
sz
);
//! Set alignment the memory will be aligned with this number
void
setAlignment
(
size_t
align
);
public:
//! allocate memory
virtual
bool
allocate
(
size_t
sz
);
//! destroy memory
virtual
void
destroy
();
//! copy memory
virtual
bool
copy
(
memory
&
m
);
//! the the size of the allocated memory
virtual
size_t
size
();
//! resize the memory allocated
virtual
bool
resize
(
size_t
sz
);
//! get a readable pointer with the data
virtual
void
*
getPointer
();
//! Increment the reference counter
virtual
void
incRef
()
{
ref_cnt
++
;}
//! Decrement the reference counter
virtual
void
decRef
()
{
ref_cnt
--
;}
//! Return the reference counter
virtual
long
int
ref
()
{
return
ref_cnt
;
}
// Default constructor
PtrMemory
()
:
spm
(
0
),
dm
(
NULL
),
sz
(
0
),
ref_cnt
(
0
)
{
#ifdef MEMLEAK_CHECK
if
(
process_to_print
==
process_v_cl
)
std
::
cout
<<
"Creating PtrMemory: "
<<
this
<<
"
\n
"
;
#endif
};
//! Constructor, we choose a default alignment of 32 for avx
PtrMemory
(
void
*
ptr
,
size_t
sz
)
:
spm
(
sz
),
dm
(
ptr
),
sz
(
0
),
ref_cnt
(
0
)
{
#ifdef MEMLEAK_CHECK
if
(
process_to_print
==
process_v_cl
)
std
::
cout
<<
"Creating PtrMemory: "
<<
this
<<
"
\n
"
;
#endif
};
~
PtrMemory
()
{
#ifdef MEMLEAK_CHECK
if
(
process_to_print
==
process_v_cl
)
std
::
cout
<<
"Delete PtrMemory: "
<<
this
<<
"
\n
"
;
#endif
if
(
ref_cnt
==
0
)
destroy
();
else
std
::
cerr
<<
"Error: "
<<
__FILE__
<<
" "
<<
__LINE__
<<
" destroying a live object"
<<
"
\n
"
;
};
};
#endif
/* PTRMEMORY_HPP_ */
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment