Modify flora license version.
[platform/core/messaging/msg-service.git] / include / utils / MsgMemory.h
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://floralicense.org/license/
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef __MSG_MEMORY_H__
18 #define __MSG_MEMORY_H__
19
20
21 /*==================================================================================================
22                                          INCLUDE FILES
23 ==================================================================================================*/
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/mman.h>
27
28
29 /*==================================================================================================
30                                         DEFINES
31 ==================================================================================================*/
32 #define GETSP() ({ unsigned int sp; asm volatile ("mov %0,sp " : "=r"(sp) ); sp;})
33 #define BUF_SIZE                                256
34 #define PAGE_SIZE                       (1 << 12)
35 #define _ALIGN_UP(addr,size)    (((addr)+((size)-1))&(~((size)-1)))
36 #define _ALIGN_DOWN(addr,size)  ((addr)&(~((size)-1)))
37 #define PAGE_ALIGN(addr)        _ALIGN_DOWN(addr, PAGE_SIZE)
38
39
40 /*==================================================================================================
41                                         FUNCTION PROTOTYPES
42 ==================================================================================================*/
43 static inline void
44 stack_trim(void)
45 {
46         unsigned int sp;
47         char buf[BUF_SIZE];
48         FILE* file;
49         unsigned int stacktop;
50         int found = 0;
51
52         sp = GETSP();
53
54         snprintf(buf, BUF_SIZE, "/proc/%d/maps",getpid());
55         file = fopen(buf,"r");
56         while(fgets(buf, BUF_SIZE, file) != NULL) {
57                 if(strstr(buf, "[stack]")){
58                         found = 1;
59                         break;
60                 }
61         }
62         fclose(file);
63
64         if(found) {
65                 sscanf(buf,"%x-",&stacktop);
66                 if(madvise((void*)PAGE_ALIGN(stacktop), PAGE_ALIGN(sp)-stacktop, MADV_DONTNEED) < 0)
67                         perror("stack madvise fail");
68         }
69 }
70
71
72 void MsgReleaseMemory();
73
74
75 #endif // __MSG_MEMORY_H__
76