{"action":"created","comment":{"url":"https://api.github.com/repos/ARMmbed/uvisor/pulls/comments/65875898","id":65875898,"diff_hunk":"@@ -0,0 +1,215 @@\n+/*\n+ * Copyright (c) 2016, ARM Limited, All Rights Reserved\n+ * SPDX-License-Identifier: Apache-2.0\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n+ * not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+#include \"uvisor_allocator.h\"\n+\n+#include <stdio.h>\n+#include <stdlib.h>\n+#include \"uvisor-lib/uvisor-lib.h\"\n+#include \"rt_TypeDef.h\"\n+#include \"rt_Memory.h\"\n+#include <string.h>\n+\n+#define DPRINTF(...) {};\n+/* #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) */\n+\n+/* Declare this variable here, so the tier-2 allocator _always_ uses the\n+ * page size that the tier-1 allocator expects! */\n+const uint32_t __uvisor_page_size = UVISOR_PAGE_SIZE;\n+\n+/* Internal structure currently only contains the page table */\n+typedef struct {\n+    UvisorPageTable table;\n+} UvisorAllocatorInternal;\n+\n+static inline UvisorPageTable * table(UvisorAllocator allocator) {\n+    return &(((UvisorAllocatorInternal *) allocator)->table);\n+}\n+\n+UvisorAllocator uvisor_allocator_create_with_pool(\n+    void * mem,\n+    size_t bytes)\n+{\n+    UvisorAllocatorInternal * allocator = mem;\n+    /* Signal that this is non-page allocated memory */\n+    allocator->table.page_size = bytes;\n+    allocator->table.page_count = 0;\n+    /* The MEMP structure must be placed AFTER table.page_origins[0] !!! */\n+    size_t offset = offsetof(UvisorAllocatorInternal, table.page_origins) + sizeof(void *);\n+    /* Create MEMP structure inside the memory */\n+    if (rt_init_mem(mem + offset, bytes - offset)) {\n+        /* Abort if failed */\n+        DPRINTF(\"uvisor_allocator_create_with_pool: MEMP allocator creation failed\\n\\n\");\n+        return NULL;\n+    }\n+    /* Remember the MEMP pointer though */\n+    allocator->table.page_origins[0] = mem + offset;\n+    DPRINTF(\"uvisor_allocator_create_with_pool: Created MEMP allocator %p with offset %d\\n\\n\", mem + offset, offset);\n+    return allocator;\n+}\n+\n+UvisorAllocator uvisor_allocator_create_with_pages(\n+    size_t size,\n+    size_t maximum_malloc_size)\n+{\n+    /* Additional overhead for each block */\n+    const size_t block_overhead = 2 * sizeof(MEMP);\n+    size_t page_count = 0;\n+\n+    /* Calcutate the required total size considering page size and overhead */\n+    for (int request = size; request > 0; request -= UVISOR_PAGE_SIZE, page_count++)\n+    {\n+        /* Adjust the requested and total sizes */\n+        request += block_overhead;\n+        size += block_overhead;\n+    }\n+    DPRINTF(\"uvisor_allocator_create_with_pages: Requesting %u pages for at least %uB\\n\", page_count, size);\n+\n+    /* Compute the maximum allocation within our blocks */\n+    size_t maximum_allocation_size = UVISOR_PAGE_SIZE - block_overhead;\n+    /* If the required maximum allocation is larger than we can provide, abort */\n+    if (maximum_malloc_size > maximum_allocation_size) {\n+        DPRINTF(\"uvisor_allocator_create_with_pages: Maximum allocation request %uB is larger then available %uB\\n\\n\", maximum_malloc_size, maximum_allocation_size);\n+        return NULL;\n+    }\n+\n+    /* Compute the require memory size for the page table */\n+    size_t allocator_type_size = sizeof(UvisorAllocatorInternal);\n+    /* Add size for each additional page */\n+    allocator_type_size += (page_count - 1) * sizeof(void *);\n+    /* Allocate this much memory */\n+    UvisorAllocatorInternal * const allocator = malloc(allocator_type_size);\n+    /* If malloc failed, abort */\n+    if (allocator == NULL) {\n+        DPRINTF(\"uvisor_allocator_create_with_pages: allocator_t failed to be allocated!\\n\\n\");","path":"api/rtx/src/uvisor_allocator.c","position":97,"original_position":97,"commit_id":"1cd9d5888f8ff552dad8305bb0a42e0042d71a85","original_commit_id":"3f7cb2b0bb433680ab87c539d557b092ec91b3bf","user":{"login":"niklas-arm","id":15176141,"avatar_url":"https://avatars.githubusercontent.com/u/15176141?v=3","gravatar_id":"","url":"https://api.github.com/users/niklas-arm","html_url":"https://github.com/niklas-arm","followers_url":"https://api.github.com/users/niklas-arm/followers","following_url":"https://api.github.com/users/niklas-arm/following{/other_user}","gists_url":"https://api.github.com/users/niklas-arm/gists{/gist_id}","starred_url":"https://api.github.com/users/niklas-arm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niklas-arm/subscriptions","organizations_url":"https://api.github.com/users/niklas-arm/orgs","repos_url":"https://api.github.com/users/niklas-arm/repos","events_url":"https://api.github.com/users/niklas-arm/events{/privacy}","received_events_url":"https://api.github.com/users/niklas-arm/received_events","type":"User","site_admin":false},"body":"Cos history.","created_at":"2016-06-06T11:39:09Z","updated_at":"2016-06-06T11:39:09Z","html_url":"https://github.com/ARMmbed/uvisor/pull/231#discussion_r65875898","pull_request_url":"https://api.github.com/repos/ARMmbed/uvisor/pulls/231","_links":{"self":{"href":"https://api.github.com/repos/ARMmbed/uvisor/pulls/comments/65875898"},"html":{"href":"https://github.com/ARMmbed/uvisor/pull/231#discussion_r65875898"},"pull_request":{"href":"https://api.github.com/repos/ARMmbed/uvisor/pulls/231"}}},"pull_request":{"url":"https://api.github.com/repos/ARMmbed/uvisor/pulls/231","id":72017346,"html_url":"https://github.com/ARMmbed/uvisor/pull/231","diff_url":"https://github.com/ARMmbed/uvisor/pull/231.diff","patch_url":"https://github.com/ARMmbed/uvisor/pull/231.patch","issue_url":"https://api.github.com/repos/ARMmbed/uvisor/issues/231","number":231,"state":"open","locked":false,"title":"3. Add tier-2 allocator and initialization","user":{"login":"niklas-arm","id":15176141,"avatar_url":"https://avatars.githubusercontent.com/u/15176141?v=3","gravatar_id":"","url":"https://api.github.com/users/niklas-arm","html_url":"https://github.com/niklas-arm","followers_url":"https://api.github.com/users/niklas-arm/followers","following_url":"https://api.github.com/users/niklas-arm/following{/other_user}","gists_url":"https://api.github.com/users/niklas-arm/gists{/gist_id}","starred_url":"https://api.github.com/users/niklas-arm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niklas-arm/subscriptions","organizations_url":"https://api.github.com/users/niklas-arm/orgs","repos_url":"https://api.github.com/users/niklas-arm/repos","events_url":"https://api.github.com/users/niklas-arm/events{/privacy}","received_events_url":"https://api.github.com/users/niklas-arm/received_events","type":"User","site_admin":false},"body":"This adds the required glue code for connecting the tier-2 with the tier-1 allocator incl. initialization of heaps on first allocation.\r\nAlso adds mutex protection to process heap via extended box index.\r\n\r\nNote: This builds directly on top of #230 and depends on it!\r\n\r\ncc @Patater @AlessandroA @meriac","created_at":"2016-05-31T13:41:08Z","updated_at":"2016-06-06T11:39:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"53d4d99dc145d3be8387b366595d9a8f904571d2","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ARMmbed/uvisor/pulls/231/commits","review_comments_url":"https://api.github.com/repos/ARMmbed/uvisor/pulls/231/comments","review_comment_url":"https://api.github.com/repos/ARMmbed/uvisor/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ARMmbed/uvisor/issues/231/comments","statuses_url":"https://api.github.com/repos/ARMmbed/uvisor/statuses/1cd9d5888f8ff552dad8305bb0a42e0042d71a85","head":{"label":"niklas-arm:feature/tier-2-allocator","ref":"feature/tier-2-allocator","sha":"1cd9d5888f8ff552dad8305bb0a42e0042d71a85","user":{"login":"niklas-arm","id":15176141,"avatar_url":"https://avatars.githubusercontent.com/u/15176141?v=3","gravatar_id":"","url":"https://api.github.com/users/niklas-arm","html_url":"https://github.com/niklas-arm","followers_url":"https://api.github.com/users/niklas-arm/followers","following_url":"https://api.github.com/users/niklas-arm/following{/other_user}","gists_url":"https://api.github.com/users/niklas-arm/gists{/gist_id}","starred_url":"https://api.github.com/users/niklas-arm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niklas-arm/subscriptions","organizations_url":"https://api.github.com/users/niklas-arm/orgs","repos_url":"https://api.github.com/users/niklas-arm/repos","events_url":"https://api.github.com/users/niklas-arm/events{/privacy}","received_events_url":"https://api.github.com/users/niklas-arm/received_events","type":"User","site_admin":false},"repo":{"id":56140266,"name":"uvisor","full_name":"niklas-arm/uvisor","owner":{"login":"niklas-arm","id":15176141,"avatar_url":"https://avatars.githubusercontent.com/u/15176141?v=3","gravatar_id":"","url":"https://api.github.com/users/niklas-arm","html_url":"https://github.com/niklas-arm","followers_url":"https://api.github.com/users/niklas-arm/followers","following_url":"https://api.github.com/users/niklas-arm/following{/other_user}","gists_url":"https://api.github.com/users/niklas-arm/gists{/gist_id}","starred_url":"https://api.github.com/users/niklas-arm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niklas-arm/subscriptions","organizations_url":"https://api.github.com/users/niklas-arm/orgs","repos_url":"https://api.github.com/users/niklas-arm/repos","events_url":"https://api.github.com/users/niklas-arm/events{/privacy}","received_events_url":"https://api.github.com/users/niklas-arm/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/niklas-arm/uvisor","description":"mbed OS uVisor -- device security layer for ARMv7M microcontrollers","fork":true,"url":"https://api.github.com/repos/niklas-arm/uvisor","forks_url":"https://api.github.com/repos/niklas-arm/uvisor/forks","keys_url":"https://api.github.com/repos/niklas-arm/uvisor/keys{/key_id}","collaborators_url":"https://api.github.com/repos/niklas-arm/uvisor/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/niklas-arm/uvisor/teams","hooks_url":"https://api.github.com/repos/niklas-arm/uvisor/hooks","issue_events_url":"https://api.github.com/repos/niklas-arm/uvisor/issues/events{/number}","events_url":"https://api.github.com/repos/niklas-arm/uvisor/events","assignees_url":"https://api.github.com/repos/niklas-arm/uvisor/assignees{/user}","branches_url":"https://api.github.com/repos/niklas-arm/uvisor/branches{/branch}","tags_url":"https://api.github.com/repos/niklas-arm/uvisor/tags","blobs_url":"https://api.github.com/repos/niklas-arm/uvisor/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/niklas-arm/uvisor/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/niklas-arm/uvisor/git/refs{/sha}","trees_url":"https://api.github.com/repos/niklas-arm/uvisor/git/trees{/sha}","statuses_url":"https://api.github.com/repos/niklas-arm/uvisor/statuses/{sha}","languages_url":"https://api.github.com/repos/niklas-arm/uvisor/languages","stargazers_url":"https://api.github.com/repos/niklas-arm/uvisor/stargazers","contributors_url":"https://api.github.com/repos/niklas-arm/uvisor/contributors","subscribers_url":"https://api.github.com/repos/niklas-arm/uvisor/subscribers","subscription_url":"https://api.github.com/repos/niklas-arm/uvisor/subscription","commits_url":"https://api.github.com/repos/niklas-arm/uvisor/commits{/sha}","git_commits_url":"https://api.github.com/repos/niklas-arm/uvisor/git/commits{/sha}","comments_url":"https://api.github.com/repos/niklas-arm/uvisor/comments{/number}","issue_comment_url":"https://api.github.com/repos/niklas-arm/uvisor/issues/comments{/number}","contents_url":"https://api.github.com/repos/niklas-arm/uvisor/contents/{+path}","compare_url":"https://api.github.com/repos/niklas-arm/uvisor/compare/{base}...{head}","merges_url":"https://api.github.com/repos/niklas-arm/uvisor/merges","archive_url":"https://api.github.com/repos/niklas-arm/uvisor/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/niklas-arm/uvisor/downloads","issues_url":"https://api.github.com/repos/niklas-arm/uvisor/issues{/number}","pulls_url":"https://api.github.com/repos/niklas-arm/uvisor/pulls{/number}","milestones_url":"https://api.github.com/repos/niklas-arm/uvisor/milestones{/number}","notifications_url":"https://api.github.com/repos/niklas-arm/uvisor/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/niklas-arm/uvisor/labels{/name}","releases_url":"https://api.github.com/repos/niklas-arm/uvisor/releases{/id}","deployments_url":"https://api.github.com/repos/niklas-arm/uvisor/deployments","created_at":"2016-04-13T09:50:01Z","updated_at":"2016-04-13T09:50:02Z","pushed_at":"2016-06-06T09:34:48Z","git_url":"git://github.com/niklas-arm/uvisor.git","ssh_url":"git@github.com:niklas-arm/uvisor.git","clone_url":"https://github.com/niklas-arm/uvisor.git","svn_url":"https://github.com/niklas-arm/uvisor","homepage":"","size":5204,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ARMmbed:dev","ref":"dev","sha":"f4cfcae2c600dce39955cbfad9dd6c84095f4739","user":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"repo":{"id":27869517,"name":"uvisor","full_name":"ARMmbed/uvisor","owner":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ARMmbed/uvisor","description":"mbed OS uVisor -- device security layer for ARMv7M microcontrollers","fork":false,"url":"https://api.github.com/repos/ARMmbed/uvisor","forks_url":"https://api.github.com/repos/ARMmbed/uvisor/forks","keys_url":"https://api.github.com/repos/ARMmbed/uvisor/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ARMmbed/uvisor/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ARMmbed/uvisor/teams","hooks_url":"https://api.github.com/repos/ARMmbed/uvisor/hooks","issue_events_url":"https://api.github.com/repos/ARMmbed/uvisor/issues/events{/number}","events_url":"https://api.github.com/repos/ARMmbed/uvisor/events","assignees_url":"https://api.github.com/repos/ARMmbed/uvisor/assignees{/user}","branches_url":"https://api.github.com/repos/ARMmbed/uvisor/branches{/branch}","tags_url":"https://api.github.com/repos/ARMmbed/uvisor/tags","blobs_url":"https://api.github.com/repos/ARMmbed/uvisor/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ARMmbed/uvisor/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ARMmbed/uvisor/git/refs{/sha}","trees_url":"https://api.github.com/repos/ARMmbed/uvisor/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ARMmbed/uvisor/statuses/{sha}","languages_url":"https://api.github.com/repos/ARMmbed/uvisor/languages","stargazers_url":"https://api.github.com/repos/ARMmbed/uvisor/stargazers","contributors_url":"https://api.github.com/repos/ARMmbed/uvisor/contributors","subscribers_url":"https://api.github.com/repos/ARMmbed/uvisor/subscribers","subscription_url":"https://api.github.com/repos/ARMmbed/uvisor/subscription","commits_url":"https://api.github.com/repos/ARMmbed/uvisor/commits{/sha}","git_commits_url":"https://api.github.com/repos/ARMmbed/uvisor/git/commits{/sha}","comments_url":"https://api.github.com/repos/ARMmbed/uvisor/comments{/number}","issue_comment_url":"https://api.github.com/repos/ARMmbed/uvisor/issues/comments{/number}","contents_url":"https://api.github.com/repos/ARMmbed/uvisor/contents/{+path}","compare_url":"https://api.github.com/repos/ARMmbed/uvisor/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ARMmbed/uvisor/merges","archive_url":"https://api.github.com/repos/ARMmbed/uvisor/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ARMmbed/uvisor/downloads","issues_url":"https://api.github.com/repos/ARMmbed/uvisor/issues{/number}","pulls_url":"https://api.github.com/repos/ARMmbed/uvisor/pulls{/number}","milestones_url":"https://api.github.com/repos/ARMmbed/uvisor/milestones{/number}","notifications_url":"https://api.github.com/repos/ARMmbed/uvisor/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ARMmbed/uvisor/labels{/name}","releases_url":"https://api.github.com/repos/ARMmbed/uvisor/releases{/id}","deployments_url":"https://api.github.com/repos/ARMmbed/uvisor/deployments","created_at":"2014-12-11T11:59:50Z","updated_at":"2016-06-04T08:33:24Z","pushed_at":"2016-06-06T09:34:48Z","git_url":"git://github.com/ARMmbed/uvisor.git","ssh_url":"git@github.com:ARMmbed/uvisor.git","clone_url":"https://github.com/ARMmbed/uvisor.git","svn_url":"https://github.com/ARMmbed/uvisor","homepage":"","size":6430,"stargazers_count":58,"watchers_count":58,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":33,"mirror_url":null,"open_issues_count":29,"forks":33,"open_issues":29,"watchers":58,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ARMmbed/uvisor/pulls/231"},"html":{"href":"https://github.com/ARMmbed/uvisor/pull/231"},"issue":{"href":"https://api.github.com/repos/ARMmbed/uvisor/issues/231"},"comments":{"href":"https://api.github.com/repos/ARMmbed/uvisor/issues/231/comments"},"review_comments":{"href":"https://api.github.com/repos/ARMmbed/uvisor/pulls/231/comments"},"review_comment":{"href":"https://api.github.com/repos/ARMmbed/uvisor/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ARMmbed/uvisor/pulls/231/commits"},"statuses":{"href":"https://api.github.com/repos/ARMmbed/uvisor/statuses/1cd9d5888f8ff552dad8305bb0a42e0042d71a85"}}}}
{"actor":{"display_login":"niklas-arm"}}